router.beforeEach((to, from, next) => {
console.log(router.app.$store.state.isLogin)
if(!userinfo && to.path.indexOf('login') == -1){
return next('/login')
}else{
return next()
}
});
回答:
因为使用的beforeEach,所以这个时候组件应该还没挂载,你可以通过console.log(router.app.$children.length)
看到length为0,所以不能直接通过app获取store中的值。
我找了一下store,你可以这样获取
isLogin = router.app.$options.components.App.store.state.isLogin
因为你要获取是否登录,其实你也可以把登录情况存在cookie里,这样可以通过cookie获取isLogin。
回答:
直接 import,然后取 isLogin
e.g.
// import 项目中实际路径
import store from '../store/';
console.log(store.state. isLogin);
找了个例子给你看 代码
回答:
你在main.js中import store没有,引入之后直接如下面这样获取就可以了
store.state.isLogin
回答:
首先确定你的这段拦截器代码,是写在main.js里的。然后引入vuex的store
在拦截器里,先输出store这个对象看看,里面应该会有个 getters 对象,你所定义的state,都会在里面。
试试 store.getters.isLogin
回答:
import 你写的 store
直接取 store.state.isLogin即可