路由守卫
一、全局路由守卫
所谓全局路由守卫,就是小区大门,整个小区就这一个大门,你想要进入其中任何一个房子,都需要经过这个大门的检查
- 全局路由守卫有个两个:
- 全局前置守卫
- 全局后置守卫
在路由文件(router)中:
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
mode:'history', // 路由模式 hash history abstract
routes:[
{
path:'/index',
name:'index',
component:()=>import('@/components/Head'),
meta:{ // 元数据(描述数据的数据)
title:'index'
}
}
]
})
// 前置守卫:路由跳转之前
// to 要进入的路由
// from 从那个路由过来的
router.beforeEach((to, form, next)=>{
/* 必须调用 `next` */
// 动态修改网页标题
document.title = to.matched[0].meta.title
next()
})
// 全局解析守卫: 同时在所有组件内守卫和异步路由组件被解析之后 和beforeEach区别是在导航被确认之前
router.beforeResolve((to, form, next)=>{
/* 必须调用 `next` */
next()
})
// 后置守卫:路由跳转之后
router.afterEach((to, form)=>{
})
export default router
next
next(false): 中断当前的导航。如果浏览器的 URL 改变了 (可能是用户手动或者浏览器后退按钮),那么 URL 地址会重置到 from 路由对应的地址
next(’/’) 或者 next({ path: ‘/’ }): 跳转到一个不同的地址。当前的导航被中断,然后进行一个新的导航。你可以向 next 传递任意位置对象,且允许设置诸如 replace: true、name: ‘home’ 之类的选项以及任何用在 router-link 的 to prop 或 router.push 中的选项
next()// 直接进to 所指路由
next(false) // 中断当前路由
next('/route') // 跳转到一个不同的地址。当前的导航被中断,然后进行一个新的导航
next({name:"route"})
next(error) // next 的参数是一个 Error 实例,则导航会被终止且该错误会被传递给 router.onError() 注册过的回调
next(new Error('错误内容'))
router.onError((e)=>{
console.log('xxx',e)
})
二、组件路由守卫
// 组件对象
{
template:'xxx',
beforeRouteEnter(to, from, next) {
// 在渲染该组件的时候调用 不能获取组件实例this
console.log('---beforeRouteEnter-渲染该路由---')
next()
// beforeRouteEnter 是支持给 next 传递回调的唯一守卫
next(vm => {
// 通过 `vm` 访问组件实例
})
},
beforeRouteUpdate(to, from, next) {
// 在当前路由改变,但是该组件被复用时调用
console.log('---beforeRouteUpdate-该组件被复用---')
next()
},
beforeRouteLeave (to, from, next) {
// 导航离开该组件的对应路由时调用
// 通常用来禁止用户在还未保存修改前突然离开
console.log('---beforeRouteLeave-离开单前路由--')
next()
}
}
三、路由独享守卫
路由独享守卫是在路由配置页面单独给路由配置的一个守卫
const router = new VueRouter({
mode:'history', // 路由模式 hash history abstract
routes:[
{
path:'/index',
name:'index',
component:()=>import('@/components/Head'),
meta:{ // 元数据(描述数据的数据)
title:'index'
},
beforeEnter:(to, from, next) => {
console.log("--about-beforeEnter--")
next()
}
}
]
})
页面鉴权
// 鉴权页面路由
{
path:"/userinfo",
name:"userinfo",
component:()=>import('@/views/Auth'),
meta:{auth:true}
},
import store from '../store'
// 进入路由之前触发
// to 要进入的路由
// from 从那个路由过来的
router.beforeEach(function(to, from, next){
if(to.meta.auth){ //鉴权页面标记
// 进入userinfo 要判断是否登录
if(store.state.user.isLoading){ // 在登录中
// 携带参数path 如果有多个页面都需要登录验证
next({name:"Auth",query:{path:to.name}}) //跳转到鉴权页面
}else if(store.state.user.islogin){ // 登录成功
next() // 正常跳转
}else{ // 没有登录
next({name:"login"}) //跳转到登录页面
}
return
}
next()
})
<template>
<Center>
<!-- auth 鉴权 -->
验证中
</Center>
</template>
<script>
import Center from '@/components/Center'
import {mapState} from 'vuex'
export default {
components:{
Center
},
computed:mapState('user',['data','isLogin','isLoading']),
watch:{
isLogin:{ // 监听登录状态变化
immediate:true,
handler(){
this.handleLogin()
}
},
isLoading:{
immediate:true,
handler(){
this.handleLogin()
}
}
},
methods:{
handleLogin(){
if(this.isLoading) return // 是否在登录中
if(this.isLogin){ // 是否在登录成功
if(this.$route.query.path){
// 跳转this.$route.query.path 路径
this.$router.push({name:this.$route.query.path})
}else{
// 没有指定页面路由 就跳转个人中心
this.$router.push({name:'userinfo'})
}
}else{
// 跳转登录页
this.$router.push({name:'login'})
}
}
}
}
</script>