【发布时间】: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 结束(不过问题很好,有很多细节)