简体   繁体   English

Vue路由器从Vuex访问单个吸气剂

[英]Vue Router access individual getters from Vuex

I'm trying to access the currently authenticated user in my Vuex store inside my router.js file 我正在尝试访问我的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();
    }
  }
}

When I console.log(store.getters) I get an object of all the properties in state including a user object. 当我console.log(store.getters)我得到了处于状态中的所有属性的对象,包括用户对象。

However, when I try access console.log(store.getters.user) , which does exist in my store.js, it simply returns false which is the initial state of my user object on page load. 但是,当我尝试访问console.log(store.getters.user)中确实存在的console.log(store.getters.user) ,它仅返回false ,这是页面加载时用户对象的初始状态。

How come the user object is visible from state.getters but not state.getters.user ? 用户对象如何从state.getters可见,而从state.gettersstate.getters.user

This seems to me a syntax error, because getters are accessed as properties that are cached as part of the Vue's reactivity system. 在我看来,这是语法错误,因为将getter作为属性进行访问,这些属性作为Vue反应系统的一部分进行缓存。 Use to access it: 用于访问它:

this.$store.getters

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

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