【问题标题】:Is there a specific method in which I have to invoke a vuex action from a vue component?是否有一种特定的方法我必须从 vue 组件调用 vuex 操作?
【发布时间】:2019-08-15 05:47:10
【问题描述】:

当我在我的 vue 文件中调用导入的 vuex 操作时,代码会出错并破坏网站。我什至将其分解为最简单的事情(当我单击与该操作相关的按钮时,控制台会从操作中记录一个字符串)。

在我的 Vue 根目录中

import Vue from 'vue'
import './plugins/vuetify'
import App from './App.vue'
import router from './router'
import JsonCSV from 'vue-json-csv'
import { store } from './store/index'

Vue.component('downloadCsv', JsonCSV)
Vue.config.productionTip = false


new Vue({
  render: h => h(App),
  router,
  store
}).$mount('#app')

在我的.vue 文件中:

import {mapGetters,mapActions} from 'vuex'

methods: {
      ...mapActions([
        'sendDraftSelectionPost'
      ]),
  selectPlayerMethod() {
     this.sendDraftSelectionPost('hello')
  }
}

在我已导入 Vuex 的 index.js 文件中

import Vue from 'vue'
import Vuex from 'vuex'
import { debug } from 'util';
import draft from './modules/draft'

Vue.use(Vuex)
Vue.config.devtools = true

const store = new Vuex.Store({
    strict: debug,
    modules: {
        draft,
    }
})

export default store

在我的 actions.js 文件中:

const actions = {

    sendDraftSelectionPost ({ commit }, draftSelection) {
        console.log(draftSelection)
    }

}

export default actions

预期的结果应该是我在开发控制台中看到一个字符串控制台登录说“你好”

但是,我收到了一些令人讨厌的错误,如下所示: [Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'dispatch' of undefined"

我什至没有在我的代码中输入“dispatch”这个词,所以我完全糊涂了。在这里搜索并找到很多类似的帖子后,我还没有找到任何具有可靠答案或解决方案的东西。

感谢任何帮助。

【问题讨论】:

  • 你是否安装了Vuex插件,即Vue.use(Vuex)?见vuex.vuejs.org/installation.html
  • 是的,我有!不一定意味着它在正确的位置?让我编辑带有代码块的原始帖子。
  • 原帖已更新。
  • 您是否已将您的商店添加到根 Vue 实例?见vuex.vuejs.org/guide/…
  • store 是您从 store/index.js 导出的默认值。您没有导出store 符号,因此您的导入应该是import store from './store'。投票以 typo 结束(不过问题很好,有很多细节)

标签: vue.js vuex


【解决方案1】:

根据与@Phil 的对话,原始问题(不是 cmets 中所述的子问题)的解决方案是修复我在根 Vue 中从 import { store } from './store/index'import store from './store' 的导入。谢谢,@Phil!!!

【讨论】:

    猜你喜欢
    • 2019-08-16
    • 2014-10-07
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2022-01-23
    • 2010-09-06
    • 1970-01-01
    相关资源
    最近更新 更多