我为自己创建了一个 vuex 插件,它可以触发所有商店中的所有刷新操作。但是你可以在我称之为刷新的地方做任何其他事情。
插入:
const onModuleAValueChange= (store) => {
store.watch(
state => state.moduleA.value,
(val, oldVal) => {
// Don't do anything on init state
if (!oldVal) return;
// This will trigger all refresh actions on all store. But you can add anything here
// Essentially does:
// store.dispatch(`moduleA/refresh`);
// store.dispatch(`moduleB/refresh`);
// store.dispatch(`moduleC/refresh`);
for (let state in store.state) {
const action = `${state}/refresh`;
// If the store does not have an refresh action ignore
if (store._actions[action]) store.dispatch(`${state}/refresh`);
}
// Additional action
store.dispatch(`moduleC/moduleC_Action`);
}
);
};
存储核心:
export const store = new Vuex.Store({
modules: {
moduleA,
moduleB,
moduleC
},
plugins: [onModuleAValueChange]
});