const whiteList = ["/login", "/loginpassword"];
// 全局路由守衛(wèi)轰枝,如果不是登錄狀態(tài)捅彻,則到登錄頁面
router.beforeEach((to, from, next) => {
if (sessionStorage.token) {
next();
} else {
if (to.path === "/login" || whiteList.indexOf(to.path) !== -1) {
next();
} else {
next("/login");
}
}
});
// 路由守衛(wèi)要寫在router掛載到VUE之前;
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
注意事項(xiàng)
1鞍陨、to.path === "/login"步淹;這個判斷必須要有的,否則會出現(xiàn)死循環(huán)诚撵。
2贤旷、路由守衛(wèi)的方法要在router掛載到VUE之前。否則報錯砾脑,next is not a function;