尝试从vue.js组件分派动作时,vuex未知动作类型

时间:2019-01-16 19:24:38

标签: vue.js vuejs2 components vuex vuex-modules

我在另一个项目中使用laravel,vue和vuex,它们的代码几乎相同,而且效果很好。我正在尝试使用该代码作为样板来适应在该项目中所做的事情,但我不断收到错误消息:

[vuex] unknown action type: panels/GET_PANEL

我在商店目录中有一个index.js,然后导入命名空间的商店模块以保持整洁:

import Vue from "vue";
import Vuex from "vuex";
var axios = require("axios");

import users from "./users";
import subscriptions from "./subscriptions";
import blocks from "./blocks";
import panels from "./panels";

Vue.use(Vuex);
export default new Vuex.Store({

  state: {
  },
  actions: {

  },
  mutations: {

  },
  modules: {
    users,
    subscriptions,
    blocks,
    panels
  }
})

panels.js:

 const state = {
    panel: []
  }

  const getters = {
  }
  const actions = {
    GET_PANEL : async ({ state, commit }, panel_id) => {
      let { data } = await axios.get('/api/panel/'+panel_id)
      commit('SET_PANEL', data)
    }
  }

  const mutations = {
    SET_PANEL (state, panel) {
      state.panel = panel
    }
  }

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}

下面是我的vue组件中的脚本部分:

<script>
import { mapState, mapActions } from "vuex";
export default {
  data () {
    return {
    }
  },
  mounted() {
    this.$store.dispatch('panels/GET_PANEL', 6)
  },
  computed: 
    mapState({
      panel: state => state.panels.panel
  }),
  methods: {
    ...mapActions([
      "panels/GET_PANEL"
    ])
  }
}
</script>

这是我app.js中的相关代码:

import Vue from 'vue';
import Vuex from 'vuex'
import store from './store';
Vue.use(Vuex)

const app = new Vue({
  store: store,
}).$mount('#bsrwrap')

更新:我尝试从vuex记录初始状态,然后得到:安装的挂接错误:“ ReferenceError:面板未定义。我尝试使用另一个模块存储创建另一个非常基本的组件,没有运气我检查了最新的vuex版本3.1.0。由于问题在多个模块中仍然存在,因此似乎在app.js或商店中。

1 个答案:

答案 0 :(得分:0)

具有命名空间的模块后,请使用以下映射:

...mapActions("panels", ["GET_PANEL"])

第一个参数是模块的命名空间,第二个参数是要映射的操作数组。