我在vuex上遇到了很多问题。没有明显的原因,找不到吸气剂,动作和突变。在下面的示例中,将识别并执行fetchFacilities,但是addFacility会抛出[vuex]未知操作类型:addFacility: (来自store.ts)
//...
actions: {
addFaciltiy: async function (context, fac: CFacility) {
await axios.post('/db/facilities', fac)
},
fetchFacilities: async function (context, actions) {
axios.get('/db/facilities').then((response) => response.data.forEach((element: CFacility) => {
context.commit('addFacility', element);
}));
}
//...
(来自vue组件)
import Vue from "vue";
import { CFacility } from "./model";
import { mapGetters, mapActions } from "vuex";
export default Vue.extend({
//...
methods: mapActions(["fetchFacilities", "addFacility"]),
//...
created() {
this.fetchFacilities("a");
this.addFacility(
new CFacility(//..)
);
}}
我正在使用自动生成的store.ts。我已经阅读了无数有关将模块及其名称空间弄乱的帖子,这些都是我不使用的。我为动作尝试了箭头符号,多次重写了动作,并通过调度和mapActions访问了它们。 在此先感谢:)
答案 0 :(得分:0)
尝试将省略号放在mapActions之前:
methods: ...mapActions(["fetchFacilities", "addFacility"]),
答案 1 :(得分:0)
只是一个简单的错字,多谢gracias @danielormeño