【发布时间】:2018-03-24 19:27:04
【问题描述】:
我在 vue 上下文之外有一个模块(javascript 文件)。我需要从这个 javascript 文件中调度操作。可能吗 ?如果是,那么如何?
jsfile.js(Javascript 文件)
const detail = {};
detail.validateLocation = (location) => {
// need to dispatch one action from here.
// dispatch('SET_LOCATION', {city: 'California'})
// how to dispatch action ?
}
export default detail;
action.js
export default {
SET_LOCATION: ({ commit}, data) => {
commit('SET_LOCATION', data);
},
}
store.js
import Vue from 'vue';
import Vuex from 'vuex';
import actions from './actions';
import mutations from './mutations';
import getters from './getters';
export function createStore() {
return new Vuex.Store({
modules: {},
state: {
location: null
},
actions,
mutations,
getters,
});
}
【问题讨论】: