你有写过vuex中store的插件吗?
发布于 2021-01-11 17:11:42
关注者
0
被浏览
601
1 个回答
-
Vuex
的store
接受plugins
选项,这个选项暴露出每次mutation
的钩子。Vuex
插件就是一个函数,它接收store
作为唯一参数:const myPlugin = store => { // 当 store 初始化后调用 store.subscribe((mutation, state) => { // 每次 mutation 之后调用 // mutation 的格式为 { type, payload } }); };
然后像这样使用:
const store = new Vuex.Store({ // ... plugins: [myPlugin] });