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 ...
随机推荐
- CentOS7.1安装 Vsftpd FTP 服务器
# yum install vsftpd 安装 Vsftpd FTP 编辑配置文件 ‘/etc/vsftpd/vsftpd.conf’ 用于保护 vsftpd. # vi /etc/vsftpd/vs ...
- BZOJ3211花神游历各国
BZOJ3211花神游历各国 BZOJ luogu 分块 记一个all表示该块是否全部<=1,如果all不为真就暴力修改 因为一个数被开根的次数不多,即使\(10^{12}\)只要开根6次也会变 ...
- socket编程python+c
python版: server: def socket_loop_server_function(): HOST = '192.168.56.1' PORT = 21567 sk = socket.s ...
- GeekforGeeks Trie - 键树简单介绍 - 构造 插入 和 搜索
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- Git——基本思想和工作原理(二)
核心知识点: 1.Git关注文件数据的整体是否发生变化,对更新的文件做一个快照,然后保存一个指向快照的索引,而不会关注文件数据的具体变化. 2.Git版本的更新几乎都发生在本地,不会因为没有网络而不能 ...
- mysql练习(待补充)
2.查询‘生物’课程比‘物理’课程成绩高的所有学生的学号 思路: 获取所有生物课程的人(学号,成绩)-临时表 获取所有物理课程的人(学号,成绩)-临时表 根据学号连接两个临时表: 学号 生物成绩 物理 ...
- (转载)C#格式规范
前言 之前工作中整理的一篇编码规范. 代码注释 注释约定 只在需要的地方加注释,不要为显而易见的代码加注释使用 /// 生成的xml标签格式的文档注释 方法注释 所有的方法都应该以描述这段代码的功能的 ...
- Linux电源管理(2)-Generic PM基本概念和软件架构【转】
本文转载自:http://www.wowotech.net/pm_subsystem/generic_pm_architecture.html 1. 前言 这里的Generic PM,是蜗蜗自己起的名 ...
- 利用etcd及confd实现配置自动管理
ETCD etcd 架设etcd集群 静态启动etcd集群需要每个成员在集群中知道另一个成员.在许多情况下,集群成员的IP可能提前未知.在这种情况下,可以使用etcd集群的自动发现服务.一旦etcd集 ...
- windows8.1下安装msi文件报错
新安装了win8.1系统体验体验,可是安装msi文件的软件报internal error2502和2503错误,可以换一种安装方式. 不是直接点开安装,如图所示打开命令提示符: 使用msiexec / ...