路由组件代码如下 router下index.js
const RouterModel = new Router({
routes: [...home, ...items, ...user, ...order, ...login]
});
RouterModel.beforeEach((to, from, next) => {
if (to.meta.login) { // 判断该路由是否需要登录权限
if(localStorage.getItem('access_token')){ //判断本地是否存在access_token
next();
}else {
next({
path:'/login'
})
}
}
else {
next();
}
/*如果本地 存在 token 则 不允许直接跳转到 登录页面*/
if(to.fullPath == "/login"){
if(localStorage.getItem('access_token')){
next({
path:from.fullPath
});
}else {
next();
}
}
});
在路由中配置是否需要登陆
login: true,