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路由嵌套的使用的更多相关文章

  1. 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI

    一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...

  2. vue学习路由嵌套

    1. 路由嵌套和参数传递 传参的两种形式: a.查询字符串:login?name=tom&pwd=123 {{$route.query}} ------ <li><route ...

  3. Vue系列:Vue Router 路由梳理

    Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...

  4. 04 Vue Router路由管理器

    路由的基本概念与原理 Vue Router Vue Router (官网: https://router.vuejs.org/zh/)是Vue.js 官方的路由管理器. 它和vue.js的核心深度集成 ...

  5. Vue Router路由管理器介绍

    参考博客:https://www.cnblogs.com/avon/p/5943008.html 安装介绍:Vue Router 版本说明 对于 TypeScript 用户来说,vue-router@ ...

  6. vue.js路由嵌套

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...

  7. Vue Router 路由守卫:完整的导航解析流程

    完整的导航解析流程 1 导航被触发. 2 在失活的组件里调用离开守卫. 3 调用全局的 beforeEach 守卫. 4 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+). ...

  8. Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转

    问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...

  9. vue 多级路由嵌套后打开页面是空白

    在多层路由嵌套时,一级子目录必须有一个页面并且添加一具<router-view>,否则路由跳转页面为空,没有任何显示 来自为知笔记(Wiz)

  10. Vue Router 路由实现原理

    一.概念 通过改变 URL,在不重新请求页面的情况下,更新页面视图. 二.实现方式 更新视图但不重新请求页面,是前端路由原理的核心之一,目前在浏览器环境中这一功能的实现主要有2种方式: 1.Hash  ...

随机推荐

  1. jedisLock—redis分布式锁实现(转)

    一.使用分布式锁要满足的几个条件: 系统是一个分布式系统(关键是分布式,单机的可以使用ReentrantLock或者synchronized代码块来实现) 共享资源(各个系统访问同一个资源,资源的载体 ...

  2. 智能家居DIY-空气质量检测篇-获取温度和湿度篇

    目录 智能家居DIY-空气质量检测篇-获取空气污染指数 前言 话说楼主终于升级当爸了,宝宝现在5个月了,宝宝出生的时候是冬天,正是魔都空气污染严重的时候,当时就想搞个自动开启空气净化器,由于种种原因一 ...

  3. Django在不启动server的情况下调用方法

    from django.conf import settingsfrom django import template settings.configure() a = template.Templa ...

  4. Django导出excel中文乱码解决方案

    Django官方文档有关于怎么生成csv文件的方法 import csv from django.http import HttpResponse def some_view(request): # ...

  5. Java并发之ArrayBlockingQueue

    ArrayBlockingQueue是一个由数组支持的有界阻塞队列.此队列按 FIFO(先进先出)原则对元素进行排序.队列的头部是在队列中存在时间最长的元素.队列的尾部是在队列中存在时间最短的元素.新 ...

  6. RLearning第2弹:创建数据集

    任何一门语言,数据类型和数据结构是最基础,也是最重要的,必须要学好!1.产生向量 a<-c(1,2,5,3,6,-2,4) b<-c("one","two&q ...

  7. QT修改应用程序图标

    要准备一个ico的图标,必须是ico格式,切记!! 可以用png或者其他的在线转换:http://www.easyicon.net/covert/ 用记事本 新建文件icon.rc,内容为: IDI_ ...

  8. Linux 下 Crontab 命令使用详解 定时任务

    一.  Crontab 介绍 crontab命令的功能是在一定的时间间隔调度一些命令的运行. 1.1 /etc/crontab 文件 在/etc文件夹下有一个crontab文件,这里存放有系统运行的一 ...

  9. Linux Shell 下的输出重定向

    linux 环境中支持输入输出重定向,用符号<和>来表示. 0.1和2分别表示标准输入.标准输出和标准错误信息输出, 可以用来指定需要重定向的标准输入或输出,比如 2>a.txt 表 ...

  10. 每天一个Linux命令(16)which命令

    which命令用于查找并显示给定命令的绝对路径. 环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PATH设置的目录里查找符合条件的文件.也就是说,使用which命令,就 ...