需要使用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;