gpt4 book ai didi

javascript - Vuex:功能范围模块中的未知 setter/getter

转载 作者:行者123 更新时间:2023-11-30 19:52:45 25 4
gpt4 key购买 nike

我第一次在“功能范围结构”中使用 Vuex 存储,一直难以追踪为什么我得到一个 [vuex] unknown getter: $_kp/kp - (除了引用的错误之外,Vue/Vuex 并没有为此投入太多精力)。

更新:我打开了 store.subscribeAction() 以查看它是否会提供更多信息。这是打印的日志(我没有看到任何有用的信息,但希望它对您有所帮助)。

Action Type: $_kp/getKpIndex

Action Payload: undefined

Current State: {ob: Observer} $_kp: Object kp: "2" //<- That is what I'm trying to get - "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,
};

最佳答案

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

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

在你的情况下:

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

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

此外,正如@Ijubadr 在评论中指出的那样,我不确定在您注册了您的store 模块后是否对mapGetters 进行了评估。要解决此问题,您可能必须停止使用 mapGetters 并将您的 STORE_KEY 声明为数据,然后使用 STORE_KEY 定义计算得到的 getter在其定义中(我在下面的示例中将其重命名为 storeKey,因为它不再是常量):

computed: mapState('$_kp',{
kpIndex: 'kp'
}),

created() {
this.storeKey = '$_kp';
if (!(this.storeKey in this.$store._modules.root._children)) {
this.$store.registerModule(this.storeKey, store);
}
},

mounted() {
this.$store.dispatch('$_kp/getKpIndex');
}

关于javascript - Vuex:功能范围模块中的未知 setter/getter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54317243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com