作者:广东淡水未央 | 来源:互联网 | 2023-01-13 12:39
我在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,
});
}
Bert..
15
首先,在store.js中创建商店.
import Vue from 'vue';
import Vuex from 'vuex';
import actions from './actions';
import mutations from './mutations';
import getters from './getters';
const store = new Vuex.Store({
modules: {},
state: {
location: null
},
actions,
mutations,
getters,
});
export default store
然后,将商店导入jsfile.js并使用它.
import store from "./store"
const detail = {};
detail.validateLocation = (location) => {
// Use imported store
store.dispatch('SET_LOCATION', {city: 'California'})
}
export default detail;
假设您有一个用于创建Vue实例的主文件或索引文件,您现在可能需要将导入从导入create函数更改为简单地导入存储.
1> Bert..:
首先,在store.js中创建商店.
import Vue from 'vue';
import Vuex from 'vuex';
import actions from './actions';
import mutations from './mutations';
import getters from './getters';
const store = new Vuex.Store({
modules: {},
state: {
location: null
},
actions,
mutations,
getters,
});
export default store
然后,将商店导入jsfile.js并使用它.
import store from "./store"
const detail = {};
detail.validateLocation = (location) => {
// Use imported store
store.dispatch('SET_LOCATION', {city: 'California'})
}
export default detail;
假设您有一个用于创建Vue实例的主文件或索引文件,您现在可能需要将导入从导入create函数更改为简单地导入存储.