【发布时间】:2019-08-08 19:50:56
【问题描述】:
我是 Vue 的新手,所以我很可能误解了一些东西。我想在 App.vue 的本地函数中调用 vuex 操作,如下所示:
<template>
<div id="app">
<button @click="runFunction(1)">Test</button>
</div>
</template>
<script>
import { mapActions } from 'vuex'
export default{
data() { return { } },
methods: {
...mapActions(['doAction']),
buttonClicked: (input) => { runFunction(input) }
}
}
function runFunction(input){
doAction({ ID: input });
}
</script>
该操作调用store.js 中的突变
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
export default new Vuex.Store({
state: {
IDs: []
},
mutations: {
doAction: (state, id) => { state.IDs.push(id) }
},
actions: {
doAction: ({ commit }, id) => { commit('doAction', id) }
}
})
我还有一个 main.js 来设置 vue:
import Vue from 'vue'
import App from './App.vue'
import store from './store'
new Vue({
el: '#app',
store,
render: h => h(App)
})
我得到的错误是:
ReferenceError: doAction is not defined
at runFunction
如何在函数内调用映射的操作?版本为 Vue 2.6.10
【问题讨论】: