【发布时间】:2019-04-11 16:11:10
【问题描述】:
我正在尝试在我的 router.js 文件中访问我的 Vuex 商店中当前经过身份验证的用户
{
path: '/admin/login',
name: 'admin-login',
component: AdminLogin,
beforeEnter(to, from, next) {
console.log(store.state.user);
if(store.getters.user) {
// check if an agency is trying to log in as admin
if (!store.getters.user.attributes['custom:role_id'] === 1) {
next({ name: 'agency-dashboard', params: { agency: store.getters.user.attributes['custom:agency_id'] } });
} else {
next({ name: 'admin-dashboard' });
}
} else {
next();
}
}
}
当我console.log(store.getters) 时,我得到一个状态中所有属性的对象,包括一个用户对象。
但是,当我尝试访问我的 store.js 中确实存在的 console.log(store.getters.user) 时,它只会返回 false,这是我的用户对象在页面加载时的初始状态。
为什么从state.getters 可以看到用户对象而不是state.getters.user?
【问题讨论】:
-
“为什么用户对象从
state.getters可见,但state.getters.user不可见” - 如果它返回false,那么它是“可见的”?
标签: javascript vue.js vuex vue-router