【问题标题】:'unknown action type' with Vuex mapActionVuex mapAction 的“未知动作类型”
【发布时间】: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


【解决方案1】:

从我看到的你的代码来看,你在namespaced store module

要访问命名空间的操作,您需要使用 store 模块的名称作为第一个参数来映射它(对于任何 mapState 或 mapGetter 都应如此):

methods: {
  ...mapActions("yourModuleName", [
    'getItemDetail',
  ]),
},

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 2022-01-02
    • 2021-06-26
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多