vue.js - vuex:未知的 getter:文章

标签 vue.js vuex

我尝试实现 vuex 模块并了解用法。在尝试在我的 home.vue 中导入模块时,我找到了这个解决方案:

import { FETCH_INDEX_ARTICLES } from "@/store/types/actions.type.js";
// imports this => export const FETCH_INDEX_ARTICLES = "fetchIndexArticles"
import { mapGetters, mapActions} from 'vuex'
export default {
  name: "Home",
  data() {
      return {}
  },
  computed: {
      ...mapGetters(['articles']),
    },
  methods: {
      ...mapActions([FETCH_INDEX_ARTICLES])
  }
  created() {
      this.$store.dispatch(FETCH_INDEX_ARTICLES);
  }
};

但我得到的是

vuex.esm.js?2f62:438 [vuex] unknown action type: fetchIndexArticles
vuex.esm.js?2f62:950 [vuex] unknown getter: articles

商店/index.js

export default new Vuex.Store({
  modules: {
    articles,
  }
});

商店/模块/articles.js

const state = {
  articles: [],

};
const getters = {
  articles(state) {
    return state.articles;
  },
};
const mutations = {
  [SET_ARTICLES] (state, pArticles) {
    state.article = pArticles
    state.errors = {}
  }
}
const actions = {
  [FETCH_INDEX_ARTICLES] (context) {
    context.commit(FETCH_START)
    return ApiService
      .get('/articlelist/index/')
      .then((data) => {
        context.commit(SET_ARTICLES, data.articles);
        context.commit(FETCH_END)
      })
      .catch((response) => {
        context.commit(SET_ERROR, response.data.errors);
      })
    }
};
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}

如何正确导入vuex模块?

谢谢

最佳答案

您必须指定您的模块, 当您将模块直接导入组件时,您的方法是有效的

...mapGetters('articles', {
    article: 'articles',
})

this.article(2)

https://vuex.vuejs.org/guide/modules.html#binding-helpers-with-namespace

为了方便使用,我使用方法dispatch来执行操作

this.$store.dispatch('articles/FETCH_INDEX_ARTICLES', {anydata})

关于vue.js - vuex:未知的 getter:文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61732170/

相关文章:

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

javascript - 当商店被拆分成模块时,在 VUEX 商店中出现错误 Uncaught Error : [vuex] getters should be function but "getters.getters" is {}.

vue.js - 如何使用 VueJS 路由器链接主动样式

javascript - Vue.js继承调用父方法

javascript - 将数组传递给 Laravel View 并将该数组用作 VueJS prop

typescript - 如何向 Typescript 商店内的 Quasar 项目添加通知

vue.js - Vuex - 在 api 调用之前或之后提交状态更改?

vue.js - 如何清除 vee-validate 中的错误对象?

vuejs2 - VUE路由重定向未从其他页面关闭模式

javascript - Vuex 显示错误的 reduce 结果