vue-router路由嵌套的使用
vue-router路由嵌套的使用,以及子路由中设置默认路由:
项目结构:
在/src/App.vue文件中:
<template>
<div id="app">
<router-link to="/">首页</router-link>
<router-link to="/news">新闻</router-link>
<router-view/>
</div>
</template> <script>
export default {
name: 'App'
}
</script> <style> </style>
在HellowWorld.vue文件中:
<template>
<div class="hello">
hello
</div>
</template> <script>
export default {
beforeRouteEnter (to, from, next) {
console.log('/ beforeRouteEnter');
next();
},
beforeRouteLeave (to, from, next) {
console.log('/ beforeRouteLeave');
next();
},
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>
在News.vue文件中:
<template>
<div class="news">
<router-link to="/news/cn">国内新闻</router-link>
<router-link to="/news/inter">国际新闻</router-link>
<router-view></router-view>
</div>
</template> <script>
export default { }
</script> <style>
.news{
margin:30px auto;
width:300px;
}
</style>
在Cn.vue文件中:
<template>
<div class="cn">
国内新闻
</div>
</template> <script>
export default { }
</script> <style> </style>
在Inter.vue文件中:
<template>
<div class="inter">
inter国际
</div>
</template> <script>
export default { }
</script> <style> </style>
在/router/index.js文件中,使用路由嵌套:
import Vue from 'vue'
import Router from 'vue-router' Vue.use(Router) export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: ()=>import(/* webpackChunkName 'home' */'@/components/HelloWorld'),
beforeRouteEnter (to, from, next) {
console.log('router / beforeRouteEnter');
next();
},
beforeRouteLeave (to, from, next) {
console.log('router / beforeRouteLeave');
next();
},
beforeEnter: (to, from, next) => {
console.log('router / beforeEnter');
next();
}
},
{
path: '/news',
name: 'News',
component: ()=>import(/* webpackChunkName 'News' */'@/components/News'),
children:[
{
path:'cn',
name: 'cn',
component: ()=>import(/* webpackChunkName 'cn' */'@/components/Cn'),
},
{
path:'inter',
name: 'inter',
component: ()=>import(/* webpackChunkName 'inter' */'@/components/Inter'),
},
{path: '/news', redirect: 'cn'},
]
}, ]
})
效果如下:
vue-router路由嵌套的使用的更多相关文章
- 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI
一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...
- vue学习路由嵌套
1. 路由嵌套和参数传递 传参的两种形式: a.查询字符串:login?name=tom&pwd=123 {{$route.query}} ------ <li><route ...
- Vue系列:Vue Router 路由梳理
Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...
- 04 Vue Router路由管理器
路由的基本概念与原理 Vue Router Vue Router (官网: https://router.vuejs.org/zh/)是Vue.js 官方的路由管理器. 它和vue.js的核心深度集成 ...
- Vue Router路由管理器介绍
参考博客:https://www.cnblogs.com/avon/p/5943008.html 安装介绍:Vue Router 版本说明 对于 TypeScript 用户来说,vue-router@ ...
- vue.js路由嵌套
<!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...
- Vue Router 路由守卫:完整的导航解析流程
完整的导航解析流程 1 导航被触发. 2 在失活的组件里调用离开守卫. 3 调用全局的 beforeEach 守卫. 4 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+). ...
- Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转
问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...
- vue 多级路由嵌套后打开页面是空白
在多层路由嵌套时,一级子目录必须有一个页面并且添加一具<router-view>,否则路由跳转页面为空,没有任何显示 来自为知笔记(Wiz)
- Vue Router 路由实现原理
一.概念 通过改变 URL,在不重新请求页面的情况下,更新页面视图. 二.实现方式 更新视图但不重新请求页面,是前端路由原理的核心之一,目前在浏览器环境中这一功能的实现主要有2种方式: 1.Hash ...
随机推荐
- 1. lvs+keepalived 高可用群集
一. keepalived 工具介绍 1.专为lvs 和HA 设计的一款健康检查工具 2.支持故障自动切换 3.支持节点健康状态检查 二. keepalived 实现原理剖析 keepalived ...
- HYSBZ - 1799 self 同类分布
self 同类分布 HYSBZ - 1799 给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数.Sample Input 10 19 Sample Output 3 Hint [约束条件] ...
- 九度OJ 1254:N皇后问题 (N皇后问题、递归、回溯)
时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:765 解决:218 题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一 ...
- iOS应用的执行原理
本文转自:http://www.cnblogs.com/oc-bowen/p/6061261.html http://www.cnblogs.com/oc-bowen/p/6061178.html 一 ...
- 关于date和String互相转换的问题
其实原理很简单,就是将String类型的变量使用SimpleDateFormat来转换成Date,然后用getTime()方法比较 SimpleDateFormat sdf = new SimpleD ...
- Linux expect介绍和用法
expect时用与提供自动交互的工具.比如如果想要用ssh登陆服务器,每次都输入密码你觉得麻烦,那你就可以使用expect来做自动交互,这样的话就不用每次都输入密码了. 先看例子: #!/usr/bi ...
- SpringBoot学习笔记(2):引入Spring Security
SpringBoot学习笔记(2):用Spring Security来保护你的应用 快速开始 本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程. 参考资料: ...
- url信息
var protocol = window.location.protocol; // "http:" var host = window.location.host; //&qu ...
- [原创]java WEB学习笔记11:HttpServlet(HttpServletRequest HttpServletRsponse) 以及关于 Servlet 小结
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Oil Deposits -----HDU1241暑假集训-搜索进阶
L - Oil Deposits Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...