路由守衛分3種:
全局守衛
路由獨享守衛
組件內的路由守衛
1.全局守衛:
beforeEach
beforeResolve
afterEach:
一般放在main.js文件內
import router from './router'
router.beforeEach(to, from, next) => {
next()
}
router.afterEach(to, from, each) => {
next()
}
2.路由獨享守衛:
配置在路由定義對象里面:
const routers = new Router({
routers: [
{
path: '/',
component: '',
beforeEnter: (to, form, next) => {next()} //參數用法什么的都一樣,調用順序在全局前置守衛后面,所以不會被全局守衛覆蓋
}
]
})
3.組件內的路由守衛
beforeRouterEnter:在路由獨享守衛之后調用,不能獲取組件實例this
beforeRouterUpdate:
beforeRouterLeave:離開路由前調用,可以獲取this實例