vue3项目中在普通js文件里获取vuex里的值和方法
vuex测试数据
/**
* VueX模块的例子
*/
import VuexModule from "../VuexModule";
const HomePageModule = new VuexModule({
getters: {
GetTestData: state => state.TestData,
},
state: {
TestData: "普通js获取getters里的值测试666666666666666666666666666666666",
},
mutations: {
set_TestData: (state, data) => state.TestData = data,
},
actions: {
async TestDatafun({
commit
}, data) {
await commit('set_TestData', data);
},
},
});
export default HomePageModule;
在js文件种使用
import store from '@/store'
获取getters里的值
store.getters["HomePageModule/GetTestData"]
引入gettres:
store.getters['(modules)/(name)']:store.getters["HomePageModule/GetTestData"]
获取state里的值
store.state.HomePageModule.TestData
引入state:
store.state.(modules).(name):store.state.HomePageModule.TestData
引入mutations:
store.commit('(modules)/(method)', data):
store.commit('HomePageModule/set_TestData', data)
引入actions:
store.dispatch('(modules)/(method)', data)
store.dispatch('HomePageModule/TestDatafun', data)