router 导入

import Vue from 'vue'
import Router from 'vue-router'
import order from '@/view/New/order.vue'
import Son1 from '@/view/New/son1.vue'
import Son2 from '@/view/New/son2.vue'
import Son3 from '@/view/New/son3.vue' Vue.use(Router) export default new Router({
linkActiveClass: 'u-link--Active'
, // 这是链接激活时的class
routes: [
{ path: '/', redirect: '/4' },
{ path: '/4', name: 'order',redirect: '/4/son1', component: order, children: [
{ path: '/4/son1', name: "son1", component: Son1 },
{ path: '/4/son2', name: "son2", component: Son2 },
{ path: '/4/son3', name: "son3", component: Son3 }
]}
]
})

order.vue

<template>
<div class="detail">
<div class="box">
<p>父组件</p>
<router-link to="/4/son1" active-class="u-link--Active">首页</router-link>
<router-link to="/4/son2" active-class="u-link--Active">电影</router-link>
<router-link to="/4/son3" active-class="u-link--Active">关于</router-link>
</div>
<!-- 这是一个占位符,将来,通过路由规则,匹配到的组件,会被替换到 router-view 所在的位置 -->
<router-view></router-view>
</div>
</template> <style lang="less" scoped>
.detail {
margin: 5px;
padding: 10px;
border: 2px dashed salmon;
height: 400px;
background-color: #f6f6f6;
.box {
margin: 5px;
padding: 10px;
border: 2px dashed blue;
p {
color: salmon;
}
.u-link--Active {
color: aqua;
}

}
}
</style>

son.vue:

<template>
<div>
<h1>这是son1.vue 首页 子组件</h1>
</div>
</template> <style lang="less" scoped>
div {
margin: 5px;
padding: 10px;
border: 2px dashed red;
h1 {
font-size: 26px;
font-weight: 700;
color: orange;
}
}
</style>

son2:

<template>
<div>
<h1>这是son2.vue 电影 子组件</h1>
</div>
</template> <style lang="less" scoped>
div {
margin: 5px;
padding: 10px;
border: 2px dashed blue;
h1 {
font-size: 26px;
font-weight: 700;
color: red;
}
}
</style>

son3:

<template>
<div>
<h1>这是son3.vue 关于子组件</h1>
</div>
</template> <style lang="less" scoped>
div {
margin: 5px;
padding: 10px;
border: 2px dashed green;
h1 {
font-size: 26px;
font-weight: 700;
color: palevioletred;
}
}
</style>

路由规则的匹配过程

  1. 用户点击 页面的 路由链接router-link,点击的一瞬间,就会修改浏览器地址栏中的Hash地址

  2. 当 hash 地址被修改以后,会立即被 vue-router 监听到,然后进行 路由规则的 匹配;最终,找到 要显示的组件;

  3. 当 路由规则匹配成功以后,就找到了要显示的组件,然后把这个组件,替换到页面指定的路由容器router-view 中

设置路由高亮的两种方式和重定向

  1. 通过路由默认提供的 router-link-active, 为这个类添加自己的高亮样式即可;

  2. 通过路由构造函数,在传递路由配置对象的时候,提供 linkActiveClass 属性,来覆盖默认的高亮类样式;

测试:

Vue-router(3)之 router-link 和 router-view 使用的更多相关文章

  1. vue 组件开发、vue自动化工具、axios使用与router的使用(3)

    一. 组件化开发 1.1 组件[component] 在网页中实现一个功能,需要使用html定义功能的内容结构,使用css声明功能的外观样式,还要使用js定义功能的特效,因此就产生了一个功能先关的代码 ...

  2. [ vue ] quasar框架踩坑:在vue文件外导入路由,执行router.push('/')没有效果

    问题描述: 1. 如图所示的项目结构目录, axios.js 文件负责拦截全局请求和回复,我在拦截回复的代码中写了:如果服务器回复了一个401错误,则执行Router.push('/'),但是该方法失 ...

  3. 【Vue】【Router】手动跳转用 this.$router.push() 时 $router 未定义的问题

    初入Vue,手写路由跳转时的问题: toXxxRoute: () => { this.$router.push({'path': '/xxx', 'name': 'xxx'}) } 由于使用了箭 ...

  4. Vue.js 的几点总结Watchers/router key/render

    Vue.js 的几点总结,下面就是实战案例,一起来看一下. 第一招:化繁为简的Watchers 场景还原: 1 2 3 4 5 6 7 8 created(){   this.fetchPostLis ...

  5. [React Router v4] Style a Link that is Active with NavLink

    We often need to be able to apply style to navigation links based on the current route. In React Rou ...

  6. vue路由中 Navigating to current location ("/router") is not allowed

    报错原因:多次点击同一路由,导致路由被多次添加 解决方法: router/index中添加以下代码: //router/index.js Vue.use(VueRouter) //导入vue路由 co ...

  7. vue项目苹果微信端使用this.$router.go(-1)返回上一页,上一页并不会重新加载的问题

    window.addEventListener('pageshow', function(e) { // 通过persisted属性判断是否存在 BF Cache if (e.persisted) { ...

  8. [React Router] Prevent Navigation with the React Router Prompt Component

    In this lesson we'll show how to setup the Prompt component from React Router. We'll prompt with a s ...

  9. [Angular Router] Lazy loading Module with Auxiliary router

    Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. ...

  10. 聊聊 Vue 的双向数据绑定,Model 如何改变 View,View 又是如何改变 Model 的

    todo defineProperty() 参考: https://www.cnblogs.com/wangjiachen666/p/9883916.html

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-search

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  2. 前端 移动端H5页面 DEBUG

    下载网址:https://github.com/Tencent/vConsole 把这个JS复制到项目里面 然后引入到HTML中 然后在JS上面实例化一下即可 页面就会有一个绿色的,然后点击一下就可以 ...

  3. vi/vim常用操作

    什么是vim? Vim是从 vi 发展出来的一个文本编辑器.代码补全.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用,和Emacs并列成为类Unix系统用户最喜欢的文本编辑器. vim的 ...

  4. HDU - 1394 Minimum Inversion Number(线段树求逆序数---点修改)

    题意:给定一个序列,求分别将前m个数移到序列最后所得到的序列中,最小的逆序数. 分析:m范围为1~n,可得n个序列,求n个序列中最小的逆序数. 1.将序列从头到尾扫一遍,用query求每个数字之前有多 ...

  5. 完全卸载(删除)mac下自带的php

    /private/etc sudo rm -rf php-fpm.conf.defaultphp.ini php.ini.default /usr/bin sudo rm -rf php php-co ...

  6. mjpg-streamer视频服务器移植

    相关软件下载地址:http://pan.baidu.com/s/16yo8Y JPEG库的移植 对于内核linux-2.6.35,不能再使用servfox,如果要使用的需要修改大量的代码.在此选用新的 ...

  7. postgrepSQL数据库创建二级分区表

    数据库版本:version 10 以非继承的形式创建分区表,一级分区是城市,二级分区是天:指定分区有两种形式,RANGE和LIST,如果使用RANGE在创建分区语句中是通过FOR VALUES FRO ...

  8. MySQL5.7忘记密码解决方案

    第一种方法 首先是网上普遍见到的 关闭服务 windows下在C:\ProgramData(隐藏文件夹)\MySQL\MySQL Server 5.7\my.ini中的[mysqld]下添加skip- ...

  9. Python 加载mnist、cifar数据

    import tensorflow.examples.tutorials.mnist.input_data mnist = input_data.read_data_sets("MNIST_ ...

  10. softmax、交叉熵

    Softmax是用于分类过程,用来实现多分类的 它把一些输出的神经元映射到(0-1)之间的实数,并且归一化保证和为1,从而使得多分类的概率之和也刚好为1. Softmax可以分为soft和max,ma ...