全局守衛(wèi):
? ```js
? //全局前置守衛(wèi):初始化時執(zhí)行、每次路由切換前執(zhí)行
? router.beforeEach((to,from,next)=>{
? console.log('beforeEach',to,from)
? if(to.meta.isAuth){ //判斷當前路由是否需要進行權(quán)限控制
? if(localStorage.getItem('school') === 'atguigu'){ //權(quán)限控制的具體規(guī)則
? next() //放行
? }else{
? alert('暫無權(quán)限查看')
? // next({name:'guanyu'})
? }
? }else{
? next() //放行
? }
? })
? //全局后置守衛(wèi):初始化時執(zhí)行台谊、每次路由切換后執(zhí)行
? router.afterEach((to,from)=>{
? console.log('afterEach',to,from)
? if(to.meta.title){
? document.title = to.meta.title //修改網(wǎng)頁的title
? }else{
? document.title = 'vue_test'
? }
? })
? ```
獨享守衛(wèi):
? ```js
? beforeEnter(to,from,next){
? console.log('beforeEnter',to,from)
? if(to.meta.isAuth){ //判斷當前路由是否需要進行權(quán)限控制
? if(localStorage.getItem('school') === 'atguigu'){
? next()
? }else{
? alert('暫無權(quán)限查看')
? // next({name:'guanyu'})
? }
? }else{
? next()
? }
? }
? ```
組件內(nèi)守衛(wèi):
? ```js
? //進入守衛(wèi):通過路由規(guī)則蓉媳,進入該組件時被調(diào)用
? beforeRouteEnter (to, from, next) {
? },
? //離開守衛(wèi):通過路由規(guī)則,離開該組件時被調(diào)用
? beforeRouteLeave (to, from, next) {
? }
? ```