1、路由中添加meta
const routes = [
{
path: /login ,
name: login ,
component: login
},
{
path: / ,
name: Home ,
component: Home,
meta: {
needLogin: true //需要加校检判断的路由
},
},
]
2、main.js中添加判读
router.beforeEach(function(to, from, next) {
if (to.meta.needLogin) {
//页面是否登录
if (localStorage.getItem(“token”)) {
//本地存储中是否有token(uid)数据
next(); //表明已经登录
} else {
//next可以传递一个路由对象作为参数 表明需要跳转到的页面
next({
name: “login”
});
}
} else {
//表明不需要登录
next(); //继续往后走
}
});
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...
