【问题标题】:NuxtJS value from Vuex Store undefined来自 Vuex Store 的 NuxtJS 值未定义
【发布时间】:2020-03-11 23:11:56
【问题描述】:

我在 Nuxt 中创建了一个这样的 vuex 商店。

export const state = () => ({
  id: "",
  type: null,
  name: null,
});

export const mutations = {
  setProfile(state, profile) {
    state.id = profile.idStr;
    state.type = profile.type;
    state.name = profile.name;
  }
};

export const getters = {
  name(state) {
    return state.name;
  }
};

数据以store.commit("profile/setProfile", profile.data);存储并成功设置。 (值显示在 chrome dev-tools 中)

现在我正在尝试使用这样的mapGetters 方法访问我的一个组件中的属性name

import { mapGetters } from "vuex";

export default {
  computed: mapGetters({
    name: "profile/name"
  })
};

我的问题是找不到getter。

ERROR  [vuex] unknown getter: profile/name 

这种方法有什么问题?

【问题讨论】:

  • 你能给setProfile一个有效载荷的例子吗?您的商店是否在 profile.js 文件中?
  • 我已经添加了示例。存储文件名为profile.js
  • 您发现问题了吗?

标签: javascript vuex nuxt.js


【解决方案1】:

您是否需要使用mapGetters 而不是mapState

【讨论】:

  • 谢谢。我认为我们走在正确的道路上。现在我得到ERROR [vuex] unknown getter: profile/name
  • 恐怕我暂时帮不上忙,抱歉!必须睡觉。
  • 不,有问题。还是谢谢你
【解决方案2】:

截至目前(nuxt v2.14.7)的语法是

    export default {
        computed: {
            ...mapGetters({getName: 'profile/name'}),
        }
    }

那么在组件中getter可以作为

    this.getName;

【讨论】:

    猜你喜欢
    • 2018-07-25
    • 2019-12-29
    • 1970-01-01
    • 2018-09-02
    • 2020-01-17
    • 2018-09-15
    • 2018-08-01
    • 1970-01-01
    • 2020-07-11
    相关资源
    最近更新 更多