1

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);
  });
};
4

1 に答える 1

1

アクション関数は、最初のパラメーターとしてストアインスタンスを想定しています。これは通常、Vuex によって自動的に行われます。

Vue インスタンスでアクションを使用する場合、Vuex 1 でそれを達成する方法は次のとおりです。

import { setAppMode } from './actions'

new Vue({
  vuex: {
    actions: {
      setAppMode
    }
  }
})

this.setAppMode()これで、最初の引数としてストアを使用して自動的に利用できるようになりました。

store注: VMのプロパティも設定する必要があります。

import store from `./store`

// and inside the VM options:
{ 
    store: store
}

が vm インスタンスに設定されていない場合storeでも、パラメーターとして手動で渡すことができます。

this.setAppMode(store);
于 2016-10-03T18:53:36.187 に答える