简体   繁体   English

Vue 路由器 Auth-guard function 不检查 Vuex 商店 state

[英]Vue router Auth-guard function doesn't check Vuex store state

I am trying to set-up an auth-guard for a profile page.我正在尝试为个人资料页面设置 auth-guard。 I am using Vue (2.0) with vue-router and Vuex for state.我将 Vue (2.0) 与 vue-router 和 Vuex 一起用于 state。 My backend is firebase, but I don't think that's imporant here?我的后端是 firebase,但我认为这在这里不重要吗? My auth-guard function seems unable to properly fetch the requiresAuth state.我的 auth-guard function 似乎无法正确获取 requiresAuth state。

This is my auth-guard function:这是我的 auth-guard function:

  {
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()
  }
})

And the result in the browser console when changing pages is the following:更改页面时浏览器控制台中的结果如下: 浏览器控制台

The issue here, to be clear, is why the console.log contradicts the isAuthorized in the print-out of the user module's state .需要明确的是,这里的问题是为什么console.loguser模块isAuthorized的打印输出中的state相矛盾。 For reference, my store has a user and shared module.作为参考,我的商店有一个usershared模块。 The user module has a state with two members: user and isAuthorized . user模块有一个state有两个成员: userisAuthorized Hence the browser output.因此浏览器 output。

Any ideas why these two conflict?任何想法为什么这两个冲突?

This is happening because when you expand an object in the console, it will evaluate the object and its values based on the time of expanding, and not based on the time of logging it.发生这种情况是因为当您在控制台中扩展 object 时,它将根据扩展时间而不是基于记录时间来评估 object 及其值。 It should tell you when the value was evaluated, if you hover the small blue square with an !它应该告诉您何时评估该值,如果您 hover 带有! in it.在里面。 在此处输入图像描述

Meaning that, when you log the object.这意味着,当您记录 object 时。 isAuthorized is indeed false , as you can see when you log the property directly, but when you go and expand the object in the console it's true . isAuthorized确实是false ,当您直接记录属性时可以看到,但是当您 go 并在控制台中展开 object 时,它是true Because at some point after logging it, some part of your program has changed ´isAuthorized´ to true .因为在登录后的某个时间点,您的程序的某些部分已将 'isAuthorized' 更改为true

The display also varies a little whether your console is open at the time of logging the value of not.在记录 not 的值时,无论您的控制台是否打开,显示也会略有不同。 The below screenshots are based on the snippet at the end of this post, so you can try it yourself.以下截图基于本文末尾的片段,因此您可以自己尝试。

The first image is how it looks if the console is closed at the time of logging.第一个图像是在记录时控制台关闭时的外观。 Here it simply shows Object , and when you expand it, it shows state is true .这里它只是显示Object ,当你展开它时,它显示state is true

在此处输入图像描述

In the second picture, the console was open when logging, and here it shows state: false , and when you expand it, it shows state: true , because it's fetching the current state of the object. In the second picture, the console was open when logging, and here it shows state: false , and when you expand it, it shows state: true , because it's fetching the current state of the object.

在此处输入图像描述

 const object = { state: false } console.log('State:', object.state) console.log('Object:', object) object.state = true
 Open up your browsers console to see the output.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM