Vuex:功能范围模块中的未知吸气剂

时间:2019-01-22 22:13:35

标签: javascript vue.js store vuex vuex-modules

我第一次在“功能范围结构”中使用Vuex商店,一直难以追踪我为什么得到[vuex] unknown getter: $_kp/kp的原因-(Vue / Vuex不会花费很多精力除了引用的错误之外,还可以做到这一点。

更新:我打开了store.subscribeAction(),看看是否可以放弃更多信息。这是打印的日志(我看不到任何有用的日志,但希望对您有帮助)。

  

操作类型:$ _kp / getKpIndex

     

动作有效载荷:未定义

     

当前状态:{ ob :观察者} $ _kp:对象kp:“ 2” // <-这就是我想要得到的-“ 2”!

UPDATE-2:我现在也正在使用Vues Inspector,它显示以下内容:

| State
| - $_kp: object
  | - kp: "3"

| Mutation
| - payload: "3"
| - type: "$_kp/KP_DATA_UPDATED"

在此方面提供的任何帮助将不胜感激,我希望这对谁以这种方式设置其商店有用。

SomeElement.vue:

<script>
import {mapGetters} from 'vuex';
import store from '../_store';

export default {
  name  : 'KpIndexElement',
  parent: 'AVWX',

  computed: {
    ...mapGetters({
      kp: '$_kp/kp', //<-- HERE?
    }),
  },

  created() {
    const STORE_KEY = '$_kp';
    if (!(STORE_KEY in this.$store._modules.root._children)) {//<= I think there is an issue with this too
      this.$store.registerModule(STORE_KEY, store);
    }
  },

  mounted() {
    this.$store.dispatch('$_kp/getKpIndex');
  },
}
</script>

<template>
  <p><strong>Kp: </strong>{{ kp }}</p>
</template>

商店index.js

import actions      from './actions';
import getters      from './getters';
import mutations    from './mutations';

var state = {
    kp: '',
};

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations,
};

actions.js:

import api from '../_api/server';

const getKpIndex = (context) => {
  api.fetchKpData
  .then((response) => {
    console.log('fetch response: ' + response)
    context.commit('KP_DATA_UPDATED', response);
  })
  .catch((error) => {
    console.error(error);
  })
}

export default {
  getKpIndex,
}

mutations.js

const KP_DATA_UPDATED = (state, kp) => {
  state.kp = kp;
}

export default {
  KP_DATA_UPDATED,
}

...最后是getters.js

const kp = state => state.kp;

export {
  kp,
};

1 个答案:

答案 0 :(得分:1)

使用命名空间时mapGetters的语法如下:

...mapGetters('namespace', [
    'getter1',
    'getter2',
    ... // Other getters 
])

您的情况:

...mapGetters('$_kp', [
    'kp'
])

第一个参数是名称空间,第二个参数是包含要使用的getter的有效负载。

此外,正如@Ijubadr的评论中指出的那样,我不确定在注册mapGetters模块之后对store进行了评估。要解决此问题,您可能必须放弃使用mapGetters并将STORE_KEY声明为数据,然后在其定义中使用STORE_KEY定义一个计算的getter(我将其重命名为{{ 1}},因为它不再是常量):

storeKey
相关问题