自己看vue文档,难免有些地方不懂,自己整理一下,主要是vue-router具体实现的操作步骤。

安装

直接下载/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>

NPM

npm install vue-router

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

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

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

#基础

开始

HTML:

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <div id="app">
<h1>Hello App!</h1>
<p>
<!-- 使用 router-link 组件来导航. -->
<!-- 通过传入 `to` 属性指定链接. -->
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
</p>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>
</div>

<router-view>显示的当前路由的内容

<router-link to="/foo">实现路由的跳转

JavaScript:

// 0. 如果使用模块化机制编程,導入Vue和VueRouter,要调用 Vue.use(VueRouter)

// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' } // 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
] // 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
}) // 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount('#app') // 现在,应用已经启动了!

当 <router-link> 对应的路由匹配成功,将自动设置 class 属性值 .router-link-active

步骤:

1.在main.js中引入vue-router

import Vue from 'vue'
import VueRouter from 'vue-router'

2.在main.js中使用这个router插件

Vue.use(VueRouter)

3.生成这个router实例

export default new Router(){
routes:[
{
path:'/',
name:'home',
component:Home
},{
path:'/list',
name:'list',
component:List
}
]
}

4.在index.js中把路由实例传递给Vue根组件

import router from './router'

new Vue({
el:'#app',
router,
template:'<APP/>',
components:{ APP }
})

静态路由

静态配置的 ---> import ....

固定路径路由的配置

{
path:'/',
name:'home',
component:Home
},{
path:'/list',
name:'list',
component:List
}

动态路由匹配

只有动态路由可以做到分页的效果

//localhost:8080/#/list/2 index,js

{
path:'/list/:id',
name:'list',
component:List
}

list.vue

<template>
<div>{{title}}</div>
</template> <script> data(){
return {
title:""
}
} export default{
mounted(){
if(this.$route.params.id == 1){
//请求第一页数据
this.title="第一页"
}
if(this.$route.params.id == 2){
//请求第二页数据
this.title="第二页"
}
}
}
</script>

接收多个个参数

index.js

{
path:'/list/:id/name/:name',
name:'list',
component:List
}

list.vue

<template>
<div>{{title}}{{name}}</div>
</template> <script> data(){
return {
title:"",
name:""
}
} export default{
mounted(){
//结构赋值写法
const {name,id} = this.$route.params;
this.name = name; //this.name = this.$route.params.name;
if(this.$route.params.id == 1){
//请求第一页数据
this.title="第一页"
}
if(this.$route.params.id == 2){
//请求第二页数据
this.title="第二页"
}
}
}
</script>

传递不同参数 显示不同数据

项目应用:详情页

watch 响应路由参数的变化

监听路由的变化

watch:{
'$route'(to,from){
this.name = to.params.name;
}
}

在2.2中引入守卫( beforeRouteUpdate)

守卫 --> 钩子函数

beforeRouteUpdate (to, from, next) {
this.name = to.params.name;
next();//走到下一个钩子
}

嵌套路由

头部左侧不变只有内容区改变 这样的需求可以用嵌套路由来做

{
path:'/list',
name:'list',
component:List,
childeren:[{
path:'a',
component:A
}]
}

再引入一个A组件 A.vue

在list.vue组件中通过,<router-view>显示A组件的内容

在一个非app组件里面写<router-view>显示的是当前路由下子组件的内容

编程式的导航

除了<router-link>来创建a标签来定义导航链接,还可以借助router的实例方法

router.push(location,onCompelte?,onAbort?)

在Vue实例内部,可以通过$route访问路由实例,因此你可以调用this.$route.push

想要导航到不同的URL,则使用router.push方法。这个方法会向history栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的URL。

当你点击<route-link>时,这个方法会在内部调用,所以说,

点击<route-link:to="..."> ---> 声明式

等同于

调用router.push(...) ---> 编程式

可以在Header组件中跳转到list中

export defalut{
methods:{
handleClick(){
this.$router.push({
name:'list' })
}
}
}

可以在Header组件中跳转到list/123中

export defalut{
methods:{
handleClick(){
this.$router.push({
//一种方式
//path:'/list/123', //2种方式
name:'list'
params:{
id:123
}
})
}
}
}

使用router.push 或者 router.replace 里面都不能让path和params同时存在

//字符串

router.push('home');

//对象

router.push({path:'home'})

//命名的路由

router.push({name:'user',params:{userId:123}})

//带查询参数,变成/register?plan=private
router.push({path:'register',query:{plan:'private'}})

router.replace

和router.push唯一的不同就是,不会向history添加新纪录,而是替换掉当前的history记录 不能返回

router.go

命名路由

定义名字跳转

命名视图

<router-view></router-view>
<router-view name="a"></router-view>
<router-view name="b"></router-view> var app = new VueRouter({
routers:[{
path:'/',
componens:{
defaults:Foo,
a:Bar,
b:Baz
}
}] })

重定向和别名

访问/abc 重定向到/根路径

{
path:'/abc',
redirect:'/'
}

访问/bieming 实际上还是访问的根路径

{
path:'/',
name:'home',
component:Home,
alias:'/bieming'
}

路由组件传参

解耦

动态路由传id

{
path:'/list/:id',
name:'list',
component:List,
props:true
}

在list.vue中

可以直接通过props['id']获取数据

<template>
<div>{{id}}</div>
</template> <script> export default{
props['id']
}
</script>

对象模式

props:{name:"dell"}

一般是写死的字符串静态数据

函数模式

index.js

{
path:'/list/:id',
name:'list',
component:List,
props : (route)=>({
query:route.query.q
id:route.params.id
})
}

list.vue

<template>
<div>{{query}}</div>
<div>{{id}}</div>
</template> <script> export default{
props['query','id']
}
</script> 我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1rr2npw3kickj

vue路由详解的更多相关文章

  1. Vue 路由详解

    Vue 路由详解 对于前端来说,其实浏览器配合超级连接就很好的实现了路由功能.但是对于单页面应用来说,浏览器和超级连接的跳转方式已经不能适用,所以各大框架纷纷给出了单页面应用的解决路由跳转的方案. V ...

  2. react router @4 和 vue路由 详解(八)vue路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 13.vue路由守卫 a.beforeEach 全局守卫 (每个路由调用前都会触发,根据 ...

  3. react router @4 和 vue路由 详解(一)vue路由基础和使用

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 1.vue路由基础和使用 a.大概目录 我这里建了一个router文件夹,文件夹下有in ...

  4. react router @4 和 vue路由 详解(全)

    react router @4 和 vue路由 本文大纲: 1.vue路由基础和使用 2.react-router @4用法 3.什么是包容性路由?什么是排他性路由? 4.react路由有两个重要的属 ...

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

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

  6. react router @4 和 vue路由 详解(四)vue如何在路由里面定义一个子路由

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 6.vue如何在路由里面定义一个子路由? 给父路由加一个 children:[] 参考我 ...

  7. react router @4 和 vue路由 详解(二)react-router @4用法

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 2.react-router @4用法 a.大概目录      不需要像vue那样麻烦的 ...

  8. 07Vue.js快速入门-Vue路由详解

    对于前端来说,其实浏览器配合超级连接就很好的实现了路由功能.但是对于单页面应用来说,浏览器和超级连接的跳转方式已经不能适用, 所以各大框架纷纷给出了单页面应用的解决路由跳转的方案. Vue框架的兼容性 ...

  9. react router @4 和 vue路由 详解(七)react路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 12.react路由守卫? a.在之前的版本中,React Router 也提供了类似的 ...

随机推荐

  1. Docker简介以及使用docker搭建lnmp的过程(多PHP版本)

    一.Docker基础 Docker安装 Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker. 通过 uname ...

  2. 实现全选、全不选功能 JQuery

    <input type="checkbox" name="" id="checkAllChange" value="&quo ...

  3. Python全栈-magedu-2018-笔记3

    第三章 - Python 内置数据结构 分类 数值型 int.float.complex.bool 序列对象 字符串 str 列表 list tuple 键值对 集合set 字典dict 数值型 数值 ...

  4. Enable Coded UI Testing of Your Controls

    http://msdn.microsoft.com/en-us/library/hh552522.aspx AccessibleObject Class http://msdn.microsoft.c ...

  5. PHP编译报错

    //usr/lib64/liblber-2.4.so.2: error adding symbols: DSO missing from command line collect2: error: l ...

  6. 1. Scala概述

    1.1 概述 联邦理工学院洛桑(EPFL)的Martin Odersky于2001年开始设计Scala Scala是Scalable Language的简写,是一门多范式的编程语言 1.2 Scala ...

  7. WinAPI 字符及字符串函数(5): IsCharAlpha - 是否是个字母

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  8. JavaScript字符串相关

      嘛,开头来个定义好了! 首先它是JavaScript基本数据类型之一.字符串由零或多个16位Unicode字符组成的字符序列,用''或者""表示. 它有一些转义序列,例如\n ...

  9. LG1081 开车旅行

    题意 城市\(i\)的海拔高度为\(H_i\)(各不相同).定义距离为海拔差的绝对值 小\(A\)和小\(B\)轮流开车.从\(S\)起,一直向东行驶. 小\(A\)会选择第二近的城市作为目的地.小\ ...

  10. MongoDB系列----uupdate和数组

    db.collection.update( criteria, objNew, upsert, multi ) criteria : update的查询条件,类似sql update查询内where后 ...