创建插件组件 myPlugin.js:
*** Vuex 插件就是一个函数,它接收 store 作为唯一参数:
export default store => {
//通过store 使用commit 修改state状态 在插件中不允许直接修改状态——类似于组件,只能通过提交 mutation 来触发变化
store.commit('TEST_MUTATION',{name:'插件commit名字'})
store.subscribe((mutation,state)=>{ //此处监听store 中 mutation的执行
console.log("mutation === ",mutation)
})
}
插件的使用:
import Vue from 'vue'
import Vuex from 'vuex'
import myPlugin from './plugins/myPlugin'
export default new Vuex.Store({
plugins:[myPlugin ]
})