vue-router 导航守卫处理登录问题

//router/index.js

import store from "../store";
const router=new Router({...})
router. beforeEach(( to, from, next) => {
store. commit( 'GET_USERNAME');//做路由跳转前提交mutation(username)
const isLogin = store.state.userName;
if (to.name !== "login") {
if ( !isLogin) {
next({
path: "/login"
});
} else {
next();
}
} else {
sessionStorage. clear();
store. commit( 'GET_USERNAME');
next();
}
});

你可能感兴趣的:(vue-router,vue)