vue路由插件之vue-route

2020-06-12 21:18:13易采站长站整理

vue路由插件,vuer Router,使vue官方的路由管理其,和vue高度耦合

  1.vue-Router的使用  


import Vue from 'vue'
import Router from 'vue-router' //引入路由组件

Vue.use(Router)

new Router({
mode: 'history', //路由的两种模式 hash 和history 默认使history模式
routes: [
{
path: '/',
name: 'home',
component: () => import(xxx.vue)
},
{
path: '/about',
name: 'about',
component: () => import()
}
]})

  2.路由的跳转

    

this.$router.push('/path')

    this.$router.push({name:'routername'})

    路由的get方式传值

    

this.$router.push({name:'routername',query:{id:xxx}})

    路由的post方式传值

    

this.$router.push({name:'routername',params:{id:xxx}})

  3.路由的后退

    

this.$router.go(-1) 

    this.$router.back()

  4.路由的前进

    

this.$router.forward() 

  5.替换当前路由,在路由历史中不会再出现该路由

    

this.$router.replace(location)

  6.当前路由的对象属性(一定要记得是小写的$route,并且没有r)

     this.$route.path   当前路由路径 path

     this.$route.name  当前路由名称

     this.$route.params.id  post方式传参时,获取id的值

     this.$route.query.id get方式传参时获取id的值

    this.$route.hash 当前路由的hash值,带#

   7.linkActiveClass

    当前激活的路由的class类名,默认是”router-link-active”

  8.scrollBehavior 

    切换路由时页面滚动到具体位子

  9.router-link 中的tag标签,生成具体的标签的html 元素

  10.router-view 路由组件具体渲染的地方

  11.全部的路由钩子函数(导航首位)

    11.1router.beforeEach  全局前置首位

    11.2router.beforeResolve 全局解析守卫

    11.3router.afterEach 全局后置守卫

    11.4beforeEnter 路由独享守卫

    组件内守卫

    11.5beforerouteEnter 进入

    11.6beforerouteUpdate  更新

    11.7beforerouteLeave 离开


/* 全局前置守卫 */
router.beforeEach(function (to, from, next) {