【发布时间】:2021-04-28 21:32:49
【问题描述】:
我正在尝试为个人资料页面设置 auth-guard。我将 Vue (2.0) 与 vue-router 和 Vuex 一起用于状态。我的后端是firebase,但我认为这在这里不重要吗?我的 auth-guard 函数似乎无法正确获取 requiresAuth 状态。
这是我的 auth-guard 功能:
{
path: "/profile",
name: "Profile",
component: Profile,
meta: {
requiresAuth: true
}
}
];
const router = new VueRouter({
mode: "history",
base: process.env.BASE_URL,
routes
});
router.beforeEach((to, from, next) => {
console.log('-----------ROUTE GUARD PROPS--------');
console.log(store.state.user.isAuthorized);
console.log(store.state.user);
console.log('----------- END ROUTE GUARD PROPS--------');
if (to.matched.some(record => record.meta.requiresAuth)) {
if (store.state.user.isAuthorized) {
next()
} else {
next("/Login")
}
} else {
next()
}
})
这里的问题需要明确的是,为什么console.log 与user 模块的state 打印输出中的isAuthorized 相矛盾。作为参考,我的商店有一个 user 和 shared 模块。 user 模块有一个 state 有两个成员:user 和 isAuthorized。因此浏览器输出。
任何想法为什么这两个冲突?
【问题讨论】:
-
所以你问为什么
console.log(store.state.user.isAuthorized)和console.log(store.state.user)在控制台中显示两个不同的东西? -
好吧,为什么
console.log(store.state.user.isAuthorized)看起来是false但store.state.user中的isAuthorized是true。我觉得这些应该是一样的?
标签: javascript vue.js vuex