路由跳轉前,可以用導航守衛判斷是否登錄,如果登錄了就正常跳轉,沒有登錄就把路由指向登錄頁面。
router.beforeEach((to, from, next) => {
const nextRoute = [ 'api', 'my/index, 'course'];
if(nextRoute.indexOf(to.name) >= 0){
//可以在這里面判斷是否登錄了
if (!store.state.auth.IsLogin) {
vueRouter.push({name: 'login'})
}
}
if (to.name === 'login') {
if (auth.IsLogin) {
vueRouter.push({name: 'home'});
}
}
next();//一定要加上,不然不解析模板
})
to:即將進入的路由
from:即將離開的路由
next:是一個函數,一定要加上,不然不解析模板
