简体   繁体   English

状态对象未定义 Vuex 存储

[英]Vuex store is undefined for the state object

I am using vuex in my project, and in my state object i want to reach another state object.我在我的项目中使用 vuex,在我的状态对象中我想访问另一个状态对象。 To do that i created a getters in my store but i'm getting "store is undefined" error.为此,我在我的商店中创建了一个 getter,但出现“商店未定义”错误。 Here is my store:这是我的商店:

https://codesandbox.io/s/peaceful-gould-bvq2b here is the sandbox. https://codesandbox.io/s/peaceful-gould-bvq2b这里是沙箱。

const store = new Vuex.Store({
  state: {
    members: [],
    member: {},
    messages: [
      {
        id: Math.floor(Math.random() * 10000),
        owner: store.getters.getRandomMember,
        content: "Hello this is a test message",
        date: "06.03.2020 12:20"
      },
      {
        id: Math.floor(Math.random() * 10000),
        owner: store.getters.getRandomMember,
        content: "Hello this is the second message",
        createdDate: "06.03.2020 12:20"
      },
      {
        id: Math.floor(Math.random() * 10000),
        owner: store.getters.getRandomMember,
        content: "Hello this is the third message",
        createdDate: "06.03.2020 12:40"
      }
    ]
  },
  mutations: {
    SET_MEMBERS(state, membersData) {
      state.members = membersData;
    }
  },
  actions: {
    getMembers() {
      axios
        .get("https://api.github.com/orgs/github/public_members")
        .then(membersResult => {
          this.commit("SET_MEMBERS", membersResult.data);
        });
    }
  },
  getters: {
    getRandomMember: state => {
      return state.members[Math.floor(Math.random() * 30)];
    }
  },
  modules: {}
});

export default store;

The sandbox tells you what is wrong with your code.沙箱会告诉您代码有什么问题。 You're using 'store' before it was defined.您在定义之前使用了“商店”。 I'm not sure on your entity structure but I would change your code so it does something like pick a random member when you add a message.我不确定您的实体结构,但我会更改您的代码,以便在您添加消息时选择随机成员。 Members is also empty at the start so it would just pick null anyway.成员在开始时也是空的,所以无论如何它都会选择 null。

问题描述

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

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