【问题标题】:Vue Router access individual getters from VuexVue Router 从 Vuex 访问单个 getter
【发布时间】: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


【解决方案1】:

在我看来这是一个语法错误,因为 getter 是作为属性访问的,这些属性作为 Vue 反应性系统的一部分缓存。用于访问它:

this.$store.getters

【讨论】:

    猜你喜欢
    • 2016-08-10
    • 2021-04-26
    • 2017-12-21
    • 2021-07-06
    • 2017-07-25
    • 1970-01-01
    • 2018-02-10
    • 2017-06-29
    • 2019-10-28
    相关资源
    最近更新 更多