首页 > 解决方案 > Vuex getter 未定义,但 store 有正确的数据

问题描述

我正在改变数据并确认 $store 已正确更新,但 getter 仍未定义。我正在使用 vue-dev-tools 进行故障排除。我FETCH_ENDPOINTS从加载的第一个组件调用。

有趣的事实:如果我关闭浏览器并冷加载应用程序,getter 就会有数据。如果我刷新页面,getter 是未定义的。

Vuex 模块:

const getters = {
  agentEndpoints: state => state.agent.agentEndpoints
};

export const actions = {
  [FETCH_ENDPOINTS](context) {
    CcpService.getEndpoints().then(endpoints => {
      console.log("action: " + JSON.stringify(endpoints)); // This has the right data!
      context.commit(SET_ENDPOINTS, endpoints);
    });
  }
};

export const mutations = {
  [SET_ENDPOINTS](state, endpoints) {
    console.log("mutation: " + JSON.stringify(endpoints)); // This has the right data!
    state.agent.agentEndpoints = endpoints;
  }
};

标签: javascriptvue.jsvuex

解决方案


推荐阅读