关键字:router,children ,router-link,router-view,router-link-active
先理解什么是children?
后代路由为某路由中用到router-view时,会默认加载的路由中的后代路由。
eg:
这里通过点击切换tab实现动态切换路由
先看目录结构:

aslide组件:
<div>
<ul class="tabBar">
<li v-for="(item,index) in tabList" :key="index"> <router-link :to="{path:item.url}">{{item.tit}}</router-link> </li>
</ul>
<router-view class="cont"></router-view>
</div>
样式:
.tabBar {
width: 100%;
height: 90px;
display: flex;
border-bottom: 1px solid #ccc;
}
.tabBar li {
display: flex;
flex:;
height: 90px;
}
.tabBar li a {
width: 100%;
line-height: 90px;
display: block;
text-align: center;
}
.router-link-active {
color: red; /*background-color: #003399;*/
border-bottom: 1px solid red;
}
.cont {
display: flex;
justify-content: center;
align-items: center; }
js:
export default {
data() {
return {
isShow: true,
tabList: [
{ url: '/a', tit: "One" },
{ url: '/b', tit: "Two" },
{ url: '/c', tit: "Three" }
]
}
}
}
下来我们还需要对router.js路由模块进行 配置
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router);
Router.prototype.animate = 0;
const _import = file => () => import('@/views/' + file + '.vue');
const _import_ = file => () => import('@/components/' + file + '.vue');
/*
* slide:页面切换动画
* login:是否需要登陆
* */
Vue.use(Router); export default new Router({
linkActiveClass: "router-link-active",//后代路由被激活时,给路由标签添加指定样式
routes: [
{
path: '/',
name: '首页',
component: _import('home/index'),
children: [
{
path: '',
redirect: '/a'
},//访问首页的时候默认访问后代路由中的a
{
path: '/a',
component: _import_('common/a')
},
{
path: '/b',
component: _import_('common/b')
},
{
path: '/c',
component: _import_('common/c')
}]
},
{
path: "/list",
name: "list",
component: _import("home/list"),
meta: {
slide: 1,
login: true
}
},
{
path: "/search",
name: "search",
component: _import("search/index"),
meta: {
slide: 1
}
},
{
path: "/center",
name: "center",
component: _import("center/index"),
meta: {
slide: 1
}
},]
})
主要核心部分如上,我们需要注意的这个属性linkActiveClass这个属性指定了后代路由被激活时所添加的样式。
实现的思路为:当点击上述router-link中的路由时,实现动态改变router-view中的后代路由以此实现tab的切换。而后代路由中的第一项:redirect:/a意思是默认访问首页的时候,后代路由默认显示哪个
 

vue-router(二)后代路由的更多相关文章

  1. vue.js利用vue.router创建前端路由

    node.js方式: 利用node.js安装vue-router模块 cnpm install vue-router 安装完成后我们引入这个模板! 下载vue-router利用script引入方式: ...

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

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

  3. [Vue 牛刀小试]:第十四章 - 编程式导航与实现组件与 Vue Router 之间的解耦

    一.前言 在上一章的学习中,通过举例说明,我们了解了 Vue Router 中命名路由.命名视图的使用方法,以及如何通过 query 查询参数传参,或者是采用 param 传参的方式实现路由间的参数传 ...

  4. Vue躬行记(8)——Vue Router

    虽然Vue.js未提供路由功能,但是官方推出了Vue Router(即vue-router库),以插件的形式支持.它与Vue.js深度集成,可快速的创建单页应用(Single Page Applica ...

  5. [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制

    一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...

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

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

  7. [Vue 牛刀小试]:第十三章 - Vue Router 基础使用再探(命名路由、命名视图、路由传参)

    一.前言 在上一章的学习中,我们简单介绍了前端路由的概念,以及如何在 Vue 中通过使用 Vue Router 来实现我们的前端路由.但是在实际使用中,我们经常会遇到路由传参.或者一个页面是由多个组件 ...

  8. 「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 下)

    大家好,在上一篇系列文章里,我们一起学习了路由的基本配置,如何创建路由和传参,本篇文章我们一起学习下 Navigation 导航和路由守卫的相关内容. Navigation 如果要改变当前路径,我们可 ...

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

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

随机推荐

  1. static变量、static方法之间的异同

        private SchemeBean getEmptyScheme() {        SchemeBean scheme = new SchemeBean();        scheme ...

  2. CentOS 7 Nginx安装配置

    1.添加Nginx源 yum install epel-release 2.安装Nginx yum install nginx 3.启动Nginx systemctl start nginx //配置 ...

  3. Linux 本地调试Hadoop

    将Hadoop部署后,可以使用java api进行访问,但是并不能像安装完mysql后用python自带的mysql api连接那么简单. Hadoop/share目录下有Hadoop所有的jar包, ...

  4. Visual Studio 2010 C++ 属性设置基础

    在 <Visual Studio 2010 C++ 工程文件解读>中提到了C++工程中可以进行用户自定义的属性设置,如何进行属性设置呢? 下面我们来了解一下 props 文件的基本规则: ...

  5. windows向github提交代码

    随便写的,留给自己看. 一.在github上注册并建立自己的仓库http://www.cnblogs.com/keZhenxu94/p/5288488.html 二.安装windows版本git界面工 ...

  6. 前端要不要学数据结构&算法

    我们都知道前端开发工程师更多偏向 DOM 渲染和 DOM 交互操作,随之 Node 的推广前端工程师也可以完成服务端开发.对于服务端开发而言大家都觉得数据结构和算法是基础,非学不可.所以正在进行 No ...

  7. Thread的中断机制(interrupt),循环线程停止的方法

    一.中断原理 中断线程 线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡.还是等待新的任务或是继续运行至下一步,就取决于这个 ...

  8. 清理网页中的HTML

    public string ClearHtml(string text)//过滤html,js,css代码 { text = text.Trim(); if (string.IsNullOrEmpty ...

  9. EFCore

    Nuget引用 LinqKit.Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.SqlServer 然后新建类继承DbConte ...

  10. raid write back / write throught

    RAID write back指的是raid控制器能够将写入的数据写入自己的缓存中,并把它们安排到后续再执行,这样做的好处就是不需要等实际写入磁盘再返回,因此写入更快.对于数据库而言,这一点更为重要, ...