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 ...
随机推荐
- windy数(简单数位DP)
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 6306 Solved: 2810[Submit][Sta ...
- 九度OJ 1203:IP地址 (字符串处理)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3038 解决:1496 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1<=n<=500) ...
- 我的Android进阶之旅------>如何将Android源码导入Eclipse中来查看(非常实用)
Android源码下载完成的目录结构如如所示: step1:将.classpath文件拷贝到源代码的根目录 Android源码支持多种IDE,如果是针对APP层做开发的话,建议大家使用Eclipse开 ...
- Android代码绘制虚线、圆角、渐变效果图
drawable文件夹放置动画/形状/选择器等属性文件,唯一的drawable文件名,不允许写错和拼错,否则运行报错.drawable文件夹底下的xml文件可以包括的标签共18个:animation- ...
- windows下安装PyQt4
第一步:确认自己电脑上的Python版本.然后下载对应的.whl文件下载 第二步:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4上下载对应版本版本的 ...
- (3)mac下"-bash: mysql: command not found"解决方案
针对 mysql: command not found 输入命令 $ ln -s /usr/local/mysql/bin/mysql /usr/bin 假如你人品不好,被打脸了,提示你权限不够: l ...
- R语言图形base系统(一)
一般R作图有三大绘图系统:base系统.ggplot2绘图系统.lattice绘图系统. 本篇主要介绍base系统绘图时的图形参数.一般用plot()函数来完成.在R中,若 ...
- log4j中怎样将信息写入到不同的日志文件
log4j中怎样将信息写入到不同的日志文件 有没有想过为什么我们用:Logger logger = Logger.getLogger(ABC.class) ;来得到 logger? 不想只看人家的 d ...
- Data Structure Array: Given an array arr[], find the maximum j – i such that arr[j] > arr[i]
http://www.geeksforgeeks.org/given-an-array-arr-find-the-maximum-j-i-such-that-arrj-arri/ #include & ...
- JS饼状图表数据分布插件
在线演示 本地下载