一、vue-router的安装

官网文档

【官网】:https://cn.vuejs.org/v2/guide/routing.html

【router文档】:https://router.vuejs.org/zh/

导入文件时找到根目录写法:[@目录]

import router from '@/src/components/index.vue'

1.1直接下载 / CDN

https://unpkg.com/vue-router/dist/vue-router.js

Unpkg.com 提供了基于 NPM 的 CDN 链接。上面的链接会一直指向在 NPM 发布的最新版本。你也可以像 https://unpkg.com/vue-router@2.0.0/dist/vue-router.js 这样指定 版本号 或者 Tag。

在 Vue 后面加载 vue-router,它会自动安装的:

<script src="/path/to/vue.js"></script>
<script src="/path/to/vue-router.js"></script>

1.2NPM安装(重点)

npm / cnpm install vue-router

如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能:

import Vue from 'vue'
import VueRouter from 'vue-router' Vue.use(VueRouter)

如果使用全局的 script 标签,则无须如此 (手动安装)。

1.3构建开发版

如果你想使用最新的开发版,就得从 GitHub 上直接 clone,然后自己 build 一个 vue-router。

git clone https://github.com/vuejs/vue-router.git node_modules/vue-router
cd node_modules/vue-router
npm install
npm run build

二、路由代码实例(重点)

2.1简单实例

src/main.js(重点1)

[1]引入路由

[2]引入组件

[3]使用路由

[4]创建一个路由实例

[5]默认路径

[6]待控制的组件

[7]把路由投到vue实例

import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router' //[1]引入路由
import Parent from './components/parent.vue' //[2]引入组件 Vue.config.productionTip = false Vue.use(VueRouter)//[3]使用路由 var router = new VueRouter({//[4]创建一个路由实例
routes: [{
path: "/",//[5]默认路径
component:Parent //[6]待控制的组件
}]
}) new Vue({
el: '#app',
template: '<App/>',
router,//[7]把路由投到vue实例
components: {
App
}
})

App.vue(重点2)

[1]路由显示位置

<template>
<div id="app">
<img src="./assets/logo.png" width="80px">
<router-view></router-view><!-- [1]路由显示位置 -->
</div>
</template> <script>
export default {
name: 'App',
components: {},
data () {
return {}
},
}
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

parent.vue

一个简单的组件即可

<template>
<div class="parent">
<h1>路由实例</h1>
</div>
</template> <script>
export default{
name:'parent',
components:{},
data(){
return{}
} }
</script> <style scoped>
</style>

效果:注意地址栏多了个#号,即路由成功



地址栏改成:http://localhost:8080/#/app ,即访问app.vue

2.2路由控制2个及多个组件

src/main.js(主要)

[2.2]引入组件2

[2.5]非默认路径

[2.6]待控制的组件

import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router' //[1]引入路由
import Parent from './components/parent.vue'//[2]引入组件
import Hello from './components/hello.vue'//【2.2】引入组件2 Vue.config.productionTip = false Vue.use(VueRouter)//[3]使用路由 var router = new VueRouter({//[4]创建一个路由实例
routes: [{
path: "/",//[5]默认路径组件 / 后不要加parent即可
component:Parent ,//[6]待控制的组件
},{
path: "/hello",//【2.5】非默认路径
component:Hello ,//【2.6】待控制的组件
}]
}) new Vue({
el: '#app',
template: '<App/>',
router,//[7]把路由投到vue实例
components: {
App
}
})

src/app.vue

只写重点:其它略过

 <router-view></router-view><!-- [1]路由显示位置 -->

src/components/parent.vue

简单组件,略过

src/components/hello.vue

简单组件,略过

效果:mian.js【5-6】步谁是 /默认就显示谁

http://localhost:8080/#/

路由实例parent

http://localhost:8080/#/hello

路由实例hello

2.3把路由单独提取出来成一个独立文件

src/router.js【主要】

【重点】此处必须用export default 导出其它地方才能使用

import Vue from 'vue' //[0]引入vue
import VueRouter from 'vue-router' //[1]引入路由
import Parent from './components/parent.vue'//[2]引入组件
import Hello from './components/hello.vue'//[2.2]引入组件2 Vue.use(VueRouter)//[3]使用路由 //【重点】此处必须导出其它地方才能使用
export default new VueRouter({//[4]创建一个路由实例
routes: [{
path: "/",//[5]默认路径组件 / 后不要加parent即可
component:Parent ,//[6]待控制的组件
},{
path: "/hello",//[2.5]非默认路径
component:Hello ,//[2.6]待控制的组件
}]
})

src/main.js(次要)

[7]先引入上面写好的router.js

[8]把路由投到vue实例

import Vue from 'vue'
import App from './App'
import router from './router.js'//[7]先引入 Vue.config.productionTip = false new Vue({
el: '#app',
template: '<App/>',
router,//[8]把路由投到vue实例
components: {
App
}
})

src/app.vue

只写重点:其它略过

 <router-view></router-view><!-- [1]路由显示位置 -->

src/components/parent.vue

简单组件,略过

src/components/hello.vue

简单组件,略过

效果同上例。

2.4 导航(跳转)路由各组件

navList.vue(主要)

【重点】路由链接

<template>
<div>
<!-- 【重点】路由链接 -->
<router-link to='/'>parent页</router-link>
<router-link to="/hello">hello页</router-link>
</div>
</template> <script>
export default{
name:'navList',
data(){
return{
msg:'导航组件'
}
}
}
</script> <style>
</style>

在app.vue引入导航组件

<template>
<div id="app">
<img src="./assets/logo.png" width="80px">
<navList/>
<router-view></router-view><!-- [1]路由显示位置 -->
</div>
</template> <script>
import navList from './components/navList' export default {
name: 'App',
components: {
navList,
},
data () {
return {}
},
}
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

src/components/parent.vue

简单组件,略过

src/components/hello.vue

简单组件,略过

效果:点哪个导航,切换到哪个上

2.5路由嵌套

第1步,在如下目录创建三个hello.vue的子组件

/src/components/heChild/

hello1.vue、hello2.vue、hello3.vue

内容为简单组件即可例如:

<template>
<div>
hello1子组件
</div>
</template> <script>
export default{
name:'hello1',
data(){
return{}
}
}
</script> <style></style>

第2步,对src/componets/hello.vue页面布局:

布局要求:有一个左栏,内放子导航

<template>
<div>
<div id='left'>
<h1>hello子组件</h1>
<!-- 【1】以下是路由链接 注意路径to要加上子组件的文件夹-->
<router-link to="/heChild/hello1">hellow1</router-link><br/>
<router-link to="/heChild/hello2">hellow2</router-link><br/>
<router-link to="/heChild/hello3">hellow3</router-link><br/>
</div> <div id='right'>
<!-- 【2】点路由后,其视频插入的位置 -->
<router-view></router-view>
</div>
</div>
</template> <script>
export default{
name:'hello',
data(){
return{}
}
}
</script> <style scoped>
/* 【3】对页面进行布局 */
#left {
float:left;
width:280px;
min-height: 700px;
background: lightskyblue;
}
#left a{
color:black;
}
#right {
width:100%;
min-height:700px;
}
</style>

第3步,配置路由src/router.js【重点】

【3.0】以下3个为引入第1步创建的子组件

【3.1】重定向,用于显示默认子组件

【3.2】 嵌套路由重点:在hello内部加一个children数组,在内部加上路由配置

import Vue from 'vue'
import VueRouter from 'vue-router' //[1]引入路由
import Parent from './components/parent.vue'//[2]引入组件
import Hello from './components/hello.vue'//[2.2]引入组件2 import Hello1 from './components/heChild/hello1.vue'//【3.0】以下3个为引入对应的子组件
import Hello2 from './components/heChild/hello2.vue'
import Hello3 from './components/heChild/hello3.vue' Vue.use(VueRouter)//[3]使用路由 export default new VueRouter({//[4]创建一个路由实例
routes: [{
path: "/",//[5]默认路径组件 / 后不要加parent即可
component:Parent ,//[6]待控制的组件
},
{
path: "/hello",//[2.5]非默认路径
component:Hello ,//[2.6]待控制的组件
redirect:'/heChild/hello1',//【3.1】重定向,用于显示默认子组件
//【3.2】 嵌套路由重点:在hello内部加一个children数组,在内部加上路由配置
children:[
{
path:'/heChild/hello1',
component:Hello1,
},
{
path:'/heChild/hello2',
component:Hello2,
},
{
path:'/heChild/hello3',
component:Hello3,
},
] }]
})

第4步,main.js引入3的路由文件

不重要,所有代码同上例

import Vue from 'vue'
import App from './App'
import router from '@/router.js'//[7]先引入 Vue.config.productionTip = false new Vue({
el: '#app',
template: '<App/>',
router,//[8]把路由投到vue实例
components: {
App
}
})

第5步,头部导航组件,src/navList.vue

不重要,除加了样式外,大代码同上例

<template>
<div id="nav">
<!-- 【重点】路由链接 -->
<router-link to='/'>parent页</router-link>
<router-link to="/hello">hello页</router-link>
</div>
</template> <script>
export default{
name:'navList',
data(){
return{
msg:'导航组件'
}
}
}
</script> <style scoped>
#nav {
background-color: lightskyblue;
width: 100%;
height: 30px;
line-height: 30px;
}
#nav a{
color: #000000;
text-decoration:none;
}
</style>

第6步,src/App.vue

不重要,所有代码同上例

<template>
<div id="app">
<img src="./assets/logo.png" width="80px">
<navList/>
<router-view></router-view><!-- [1]路由显示位置 -->
</div>
</template> <script>
import navList from './components/navList' export default {
name: 'App',
components: {
navList,
},
data () {
return {}
},
}
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
} </style>

第7步,parent.vue同上例

不重要

<template>
<div class="parent">
<h1>路由实例parent</h1>
</div>
</template> <script> export default{
name:'parent',
components:{},
data(){
return{
list:[] //定义一个空数组用于存放接收到的数据
}
},
filters:{},
directives:{},
}
</script> <style scoped>
</style>

效果:

2.6用路由router-link传递参数

官方文档:

https://router.vuejs.org/zh/guide/essentials/passing-props.html

第1步,先建立一个src/wa.vue组件,常规组件

代码略过..

第2步,配置src/router.js

【4.0】引入第3个组件wa.vue

【4.1】路由传递参数 (/组件名/:参数名) 下一步到navList.vue里

【4.2】路由的里传参数要用到

import Vue from 'vue'
import VueRouter from 'vue-router' //[1]引入路由
import Parent from './components/parent.vue'//[2]引入组件
import Hello from './components/hello.vue'//[2.2]引入组件2 import Hello1 from './components/heChild/hello1.vue'//[3.0]以下3个为引入对应的子组件
import Hello2 from './components/heChild/hello2.vue'
import Hello3 from './components/heChild/hello3.vue' import Wa from './components/wa.vue' //【4.0】引入第3个组件 Vue.use(VueRouter)//[3]使用路由 export default new VueRouter({//[4]创建一个路由实例
routes: [
{
path: "/",//[5]默认路径组件 / 后不要加parent即可
name:'parent',
component:Parent ,//[6]待控制的组件
},
{
path: "/hello",//[2.5]非默认路径
name:'hello',
component:Hello ,//[2.6]待控制的组件
redirect:'/heChild/hello1',//[3.1]显示默认子组件
//[3.2 嵌套路由重点]在hello内部加一个children数组,在内部加上路由配置
children:[{
path:'/heChild/hello1',
name:'hello1',
component:Hello1,
},
{
path:'/heChild/hello2',
name:'hello2',
component:Hello2,
},
{
path:'/heChild/hello3',
name:'hello3',
component:Hello3,
}]
},
{
path: "/wa/:count",//【4.1】路由传递参数 (/组件名/:参数名) 下一步到navList.vue里
name:'wa',//【4.2】路由的<router-link :to="{name:'wa',params:{count:18888}}">里传参数要用到
component:Wa,//[6]待控制的组件
}, ]
})

第3步,navLink.vue里配置router-link链接的to(重点1)

【1】待传递的参数写法:

<router-link :to="{name:'wa',params:{count:18888}}">Wa页</router-link>

<template>
<div id="nav">
<!-- 路由链接 -->
<router-link to='/'>parent页</router-link>
<router-link to="/hello">hello页</router-link>
<router-link :to="{name:'wa',params:{count:18888}}">Wa页</router-link> <!-- 【1】待传递的参数 -->
</div>
</template> <script>
export default{
name:'navList',
data(){
return{
msg:'导航组件'
}
}
}
</script> <style scoped>
#nav {
background-color: lightskyblue;
width: 100%;
height: 30px;
line-height: 30px;
}
#nav a{
color: #000000;
text-decoration:none;
}
</style>

第4步,wa.vue接收参数写法(重点2)

【1】接收参数写法(注意route不是router): {{$route.params.count}}

<template>
<div>
{{$route.params.count}}<!-- 【1】获取参数 -->
</div>
</template> <script>
export default{
name:'wa',
data(){
return{}
}
}
</script> <style>
</style>

第5步,其它部分代码

main.js

App.js

同上例略过

效果:

点到wa页,将显示从第3步传过来的参数18888

2.6.2 获取多个路由router-link传的参数

1.路由配置src/router.js

其它总分略过只写重点

path: "/wa/:count/:name"

{
path: "/wa/:count/:name",//【重点】路由传递参数 (/组件名/:参数名) 下一步到navList.vue里
name:'wa',//路由的<router-link :to="{name:'wa',params:{count:18888}}">里传参数要用到
component:Wa,//待控制的组件
},

2.传递配置src/navList.vue

【1】待传递的参数,传递多个参数

<template>
<div id="nav">
<!-- 路由链接 -->
<router-link to='/'>parent页</router-link>
<router-link to="/hello">hello页</router-link>
<router-link :to="{name:'wa',params:{count:18888,name:'小明'}}">Wa页</router-link> <!-- 【1】待传递的参数,传递多个参数 -->
</div>
</template>

3.接收配置src/components/wa.vue

<template>
<div>
{{$route.params.count}}----{{$route.params.name}}<!-- 【1】获取参数,获取多个参数 -->
</div>
</template>

效果

注意地址栏参数写法

2.7 传递参数地址栏是:?参数1=1

详见官方文档:

https://router.vuejs.org/zh/guide/essentials/passing-props.html#函数模式

https://router.vuejs.org/zh/api/#router-link

2.8简单路由高亮效果

只要在navList.vue里加个样式即可:

也可以全局设置在:App.vue里设置

<style>
.router-link-active {
color:red;
}
</style>

效果:

2.9 高级设置路由高亮效果

1. hello.vue

【1】以下是路由链接 注意路径to要加上主组件,这样点到子路由里,主路由设置颜色才不会消失

<template>
<div>
<div id='left'>
<h1>hello子组件</h1>
<!-- 【1】以下是路由链接 注意路径to要加上主组件,这样点到子路由里,主路由设置颜色才不会消失-->
<router-link to="/hello/heChild/hello1">hellow1</router-link><br/>
<router-link to="/hello/heChild/hello2">hellow2</router-link><br/>
<router-link to="/hello/heChild/hello3">hellow3</router-link><br/>
</div> <div id='right'>
<!-- 点路由后,其视频插入的位置 -->
<router-view></router-view>
</div>
</div>
</template> <script>
export default{
name:'hello',
data(){
return{}
}
}
</script> <style scoped>
/* 对页面进行布局 */
#left {
float:left;
width:280px;
min-height: 700px;
background: lightskyblue;
} #right {
width:100%;
min-height:700px;
}
</style>

2. router.js

重点:【1】-【3】

import Vue from 'vue'
import VueRouter from 'vue-router' //引入路由
import Parent from './components/parent.vue'//引入组件
import Hello from './components/hello.vue'//引入组件2 import Hello1 from './components/heChild/hello1.vue'//以下3个为引入对应的子组件
import Hello2 from './components/heChild/hello2.vue'
import Hello3 from './components/heChild/hello3.vue' import Wa from './components/wa.vue' //引入第3个组件 Vue.use(VueRouter)//使用路由 export default new VueRouter({
linkActiveClass:'active',//【0】设置路由链接处理激活状态的style样式class名(默认值: "router-link-active" )
routes: [
{
path: "/",
name:'parent',
component:Parent ,
},
//【1】带子路由的hello组件配置开始
{
path: "/hello",
name:'hello',
component:Hello ,
redirect:'/hello/heChild/hello1',//【2】路径要写完整前面带上主路由 /hello/子路由路径/子路由
//【3】子路由配置开始
children:[{
path:'/hello/heChild/hello1',//【4】子路由,注意路径
name:'hello1',
component:Hello1,
},
{
path:'/hello/heChild/hello2',//【5】子路由,注意路径
name:'hello2',
component:Hello2,
},
{
path:'/hello/heChild/hello3',// 【6】子路由,注意路径
name:'hello3',
component:Hello3,
}]
},
{
path: "/wa/:count/:name",
name:'wa',
component:Wa,
}, ]
})

3. App.vue里设置全局路由处于active状态的样式

<style>
.active {
color:red;
}
</style>

效果:

2.10 HTML5的history控制路由后退步数

官网:

https://router.vuejs.org/zh/guide/essentials/history-mode.html#后端配置例子

六、Vue-Router:基础路由处理、路由提取成单独文件、路由嵌套、路由传参数、路由高亮、html5的history使用的更多相关文章

  1. webpack 提取css成单独文件

    webpack 提取css成单独文件 // 用来拼接绝对路径的方法 const {resolve} = require('path') const HtmlWebpackPlugin = requir ...

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

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

  3. vue Router——基础篇

    vue--Router简介 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用. vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路 ...

  4. Vue Router基础

    路由 安装 vue-router 起步 <router-link to="/foo">Go to Foo</router-link> <router- ...

  5. Vue技术点整理-Vue Router

    路由 Vue Router 对于单页面应用来说,如果涉及到多个页面的话,就必须要使用到路由,一般使用官方支持的 vue-router 库 一,Vue Router 在项目中的安装引用 1,在页面中使用 ...

  6. vue Router——进阶篇

    vue Router--基础篇 1.导航守卫 正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的 ...

  7. Vue Router的官方示例改造

    基于Vue Router 2018年8月的官方文档示例,改造一下,通过一个最简单的例子,解决很多初学者的一个困惑. 首先是官方文档示例代码 <!DOCTYPE html> <html ...

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

    大家好,今天的内容,我将和大家一起聊聊 Vue 路由相关的知识,如果你以前做过服务端相关的开发,那你一定会对程序的URL结构有所了解,我没记错的话也是路由映射的概念,需要进行配置. 其实前端这些框架的 ...

  9. react router @4 和 vue路由 详解(六)vue怎么通过路由传参?

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 8.vue怎么通过路由传参? a.通配符传参数 //在定义路由的时候 { path: ' ...

随机推荐

  1. KNN-学习笔记

    仅供学习使用 练习1 # coding:utf-8 # 2019/10/16 16:49 # huihui # ref: import numpy as np from sklearn import ...

  2. How to recover if NMC cound not connect

    Some times we suddently find that the NMC can not login,. You would see the sybase database error if ...

  3. 三 Spring对于延迟加载问题的解决

    Spring提供了延迟加载问题的解决方法 什么是延迟加载? 延迟加载:lazy(懒加载) 执行到该行代码的时候不会发送语句,真正使用这个对象的属性的时候才会发送sql语句进行查询. 类级别延迟加载:指 ...

  4. CentOS配置源、wget、ifconfig基础环境

    执行命令: curl http://10.200.0.14:8000/portal.cgi -X POST -d 'username=lishuai&password=test@cetc38& ...

  5. GIMP

    1. 认识GIMP 2. GIMP与Photoshop的对比 3. GIMP官方手册教程 4. 2本GIMP的外文书下载 5. 2个外部入门教程 6. 其他相关软件 1. 认识GIMP GIMP是可用 ...

  6. vue - 封装input

    input子组件 <el-input :value="value" placeholder="请输入内容" size="small" ...

  7. selenium+chrome抓取淘宝宝贝-崔庆才思路

    站点分析 源码及遇到的问题 在搜索时,会跳转到登录界面 step1:干起来! 先取cookie step2:载入cookie step3:放飞自我 关于phantomJS浏览器的问题 源码 站点分析 ...

  8. Celery的常用知识

    什么是Clelery   Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统.专注于实时处理的异步任务队列.同时也支持任务调度. Celery的架构由三部分组成,消息中间件(message ...

  9. 前端学习笔记系列一:14 vue3.X中alias的配置

    第一步: 第二步: // vue.config.js module.exports = { configureWebpack: { resolve: { alias: { 'assets': '@/a ...

  10. 简单模拟IOC容器:返回对象并能抛出异常

    本次要求:已知com.zzj.vo包下分别有Tiger.lion.Elephant三个Java源文件,请据此实现以下功能:①.自定义一个名为Component的注解,要求该注解只能用于类且代码运行时该 ...