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