vue-router的beforeEach全局守卫,
2019-02-27 本文已影响0人
LH8966
const whiteList = ["/login", "/loginpassword"];
// 全局路由守卫,如果不是登录状态,则到登录页面
router.beforeEach((to, from, next) => {
if (sessionStorage.token) {
next();
} else {
if (to.path === "/login" || whiteList.indexOf(to.path) !== -1) {
next();
} else {
next("/login");
}
}
});
// 路由守卫要写在router挂载到VUE之前;
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
注意事项
1、to.path === "/login";这个判断必须要有的,否则会出现死循环。
2、路由守卫的方法要在router挂载到VUE之前。否则报错,next is not a function;