【发布时间】:2020-01-18 10:49:08
【问题描述】:
我是 Vuex 的新手,并尝试使用 mapActions 从我的商店中获取一些数据,并将其发送到我的组件的模板中。我不断收到错误消息
[vuex] unknown action type: getItemDetail 但我不知道为什么。
在我的商店中,我尝试调度的操作是getItemDetail。我的完整商店是
import fetch from './fetch';
const url = 'items';
const defaults = {
id: '',
rating: '',
type: '',
};
const state = { ...defaults, };
const getters = {};
const actions = {
getItemDetail ({ commit, }, itemId) {
fetch.get(`${url}/${itemId}`).then((response) => {
commit('setItemDetail', { ...defaults, ...response.data, });
});
},
};
const mutations = {
setItemDetail (state, item) {
state.item = item;
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};
在我的组件中我有:
<template>
<div>
<p> {{ itemDetail }} </p>
</div>
</template>
<script>
import { mapActions } from 'vuex';
export default {
computed: {
itemDetail () {
this.getItemDetail('23451');
return this.$store.state;
},
},
methods: {
...mapActions([
'getItemDetail',
]),
},
};
</script>
Any help would be much appreciated!!!
【问题讨论】:
-
对不起,错误代码中的getItemDetail...更新了我的帖子!
-
你如何在商店中包含
actions对象? -
刚刚更新了我的问题以包含完整的商店。
-
这不是一个完整的商店,您必须导入您的导出并将其传递给
new Vuex.store()
标签: javascript vue.js vuex