vue判断是否登录,若未登录跳转登录页
需要使用vuex,在state中存储一个isLogin字段,用于判断是否登录export const store = new Vuex.Store({state: {isLogin: false}})在main.js中添加判断是否登录//判断是否登录router.beforeEach(function (to, from, next) {...
·
需要使用vuex,在state中存储一个isLogin字段,用于判断是否登录
export const store = new Vuex.Store({
state: {
isLogin: false
}
})
在main.js中添加判断是否登录
//判断是否登录
router.beforeEach(function (to, from, next) {
if (to.meta.needLogin) {
//通过查看state中的isLogin判断是否登录
if (_this.$store.state.isLogin) {
next(); //表示已经登录
} else {
//next可以传递一个路由对象作为参数 表示需要跳转到的页面
next({
name: "Login"
});
}
} else {
//表示不需要登录
next(); //继续往后走
}
});
登录页中添加修改isLogin字段状态
this.$store.state.isLogin = true;
更多推荐
已为社区贡献10条内容
所有评论(0)