简介

官网

本文主要介绍在vue工程下vue-router的使用教程。

环境:

  1. # node --verion // v12.x
  2. # npm --version // 6.x
  3. # vue --version // @vue/cli 4.3.1
  4. # npm install vue-router // version 3.x

简单使用

添加home、list、about页

创建router/index.js, 并在main.js中引用, 在app.vue中添加相应标签
router/index.js

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import Home from '../views/Home'
  4. import List from '../views/List'
  5. import Detail from '../views/Detail'
  6. import About from '../views/About'
  7. Vue.use(VueRouter)
  8. export default new VueRouter({
  9. // mode: 'history',
  10. routes: [
  11. {
  12. path: '/home',
  13. alias: '/index',
  14. name: 'home',
  15. component: Home
  16. },
  17. {
  18. path: '/list',
  19. name: 'list',
  20. component: List
  21. },
  22. {
  23. path: '/detail',
  24. name: 'detail',
  25. component: Detail
  26. },
  27. {
  28. path: '/about',
  29. name: 'about',
  30. component: About
  31. },
  32. {
  33. path: '*',
  34. name: 'NotFoundComponent',
  35. component: () => import('../views/NotFoundComponent')
  36. }
  37. ]
  38. })

main.js

  1. import router from './router'
  2. new Vue({
  3. render: h => h(App),
  4. router,
  5. }).$mount('#app')

app.vue

  1. <router-link to="/home">home</router-link>
  2. <router-link to="/list">list</router-link>
  3. <router-link to="/about">about</router-link>
  4. <router-view></router-view>

跳转

  1. this.$router.push(`/list`)
  2. this.$router.push(`/detail/${id}`)
  3. // 方式2
  4. this.$router.push({ name: 'list' })

导航及传参

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

导航(声明式/编程式)

  1. <router-link to="/home">home</router-link>
  2. // 等价于
  3. <a @click="navHome">home</a>
  4. navHome () {
  5. this.$router.push('/home')
  6. }

path 传参

  1. // /detail/200333
  2. this.$router.push(`/detail/${id}`)
  3. 需要修改router配置
  4. path: '/detail/:id',
  5. // vue.$route
  6. fullPath: "/detail/200333"
  7. params: {id: "200333"}
  8. path: "/detail/200333"
  9. query: {}

params 传参

  1. this.$router.push({name: 'detail', params: {id,}})
  2. // warn -> key=name
  3. // vue.$route
  4. fullPath: "/detail"
  5. params: {id: 200333}
  6. path: "/detail"

query 传参

  1. this.$router.push({path: 'detail', query: { id, }})
  2. // vue.$route
  3. fullPath: "/detail?id=200333"
  4. path: "/detail"
  5. query: {id: 200333}

嵌套?

导航拦截(导航守卫)

钩子函数(组件中的守卫)

  1. beforeRouteEnter (to, from, next) {
  2. // console.log(to, from, next)
  3. next()
  4. },
  5. beforeRouteUpdate (to, from, next) {
  6. // TODO
  7. next()
  8. },
  9. beforeRouteLeave (to, from, next) {
  10. // ...
  11. next()
  12. },

应用场景

  1. beforeRouteEnter (to, from, next) {
  2. // console.log(to, from, next)
  3. const { params } = to
  4. const isCreate = JSON.stringify(params) === '{}'
  5. to.meta.title = `${isCreate ? '新建' : '编辑'}地址`
  6. next()
  7. },

其他配置

重定向

  1. {
  2. path: '/',
  3. redirect: '/home'
  4. },

别名

  1. {
  2. path: '/home',
  3. alias: '/index',
  4. name: 'home',
  5. component: Home
  6. },

容错显示(匹配, 需要放置在最底部)

  1. {
  2. path: '*',
  3. name: 'NotFoundComponent',
  4. component: () => import('../views/NotFoundComponent')
  5. }

模式

  1. mode: hash|history

keep-alive

  1. <keep-alive>
  2. <router-view></router-view>
  3. </keep-alive>
  4. // meta.keepAlive
  5. {
  6. path: '/about',
  7. name: 'about',
  8. component: About,
  9. keepAlive: true
  10. },

源码解析

原生导航对象
原理分析