我有一个ApiService()
我正在抽象我的 API 调用。我想
从服务中dispatch('SET_BUSY')
进行dispatch('SET_NOT_BUSY')
应用级突变,但出现以下错误:
TypeError: dispatch is not a function. (In 'dispatch('SET_BUSY')', 'dispatch' is undefined)
/vuex/actions.js
import { ApiService } from './services';
export const setAppMode = function ({ dispatch }) {
ApiService({
noun: 'Application',
verb: 'GetMode'
}, response => {
dispatch('SET_APP_MODE', response.Data.mode);
},
dispatch);
};
/vuex/services.js
import Vue from 'vue';
export const ApiService = (options = {}, callback, dispatch) => {
let endpoint = 'localhost/api/index.php';
let parameters = options.data;
dispatch('SET_BUSY');
Vue.http.post(endpoint, parameters, []).then((promise) => {
return promise.text();
}, (promise) => {
return promise.text();
}).then(response => {
response = JSON.parse(response);
dispatch('SET_NOT_BUSY');
if (response.Result === 'ERROR') {
console.log('ERROR: ' + response.Error.Message);
}
callback(response);
});
};