路由獨享寫法:
import VueRouter from 'vue-router' Vue.use(VueRoter) const router = new VueRouter({ routes:[ {path:component:,name:,beforeEnter:((to,next,from)=>{ alert('路由獨享-組件內守衛') next()//跳轉 next(false)//不顯示此路由下的組件 })} ], mode:'history', })
組件內守衛:在組件內寫
export defalut{ data(){ return { name:'tom' } }, beforeRouteEnter((to,from,next)=>{ //beforeRouteEnter 守衛 不能 訪問 this,因為守衛在導航確認前被調用,因此即將登場的新組件還沒被創建。 //可以通過next()回調函數獲得組件實例 next(vm=>{ vm.name }) }) }