一、官方文档
1、第一步
const myPlugin = store => { // 当 store 初始化后调用 store.subscribe((mutation, state) => { // 每次 mutation 之后调用 // mutation 的格式为 { type, payload } }); };
2、第二步
const store = new Vuex.Store({ // ... plugins: [myPlugin] });
二、编写一个打印日志的插件
1、函数的书写
import _ from 'lodash'; function logger() { return function(store) { let prevState = store.state; store.subscribe((mutations, state) => { console.log('prevState', prevState); console.log(mutations); console.log('currentState', state); prevState = _.cloneDeep(state); }); }; }
2、使用
export default new Vuex.Store({ modules: { ... }, strict: true, plugins: process.NODE_ENV === 'production' "color: #ff0000">三、 vuex 数据持久化1、函数的书写
function vuexLocal() { return function(store) { const local = JSON.parse(localStorage.getItem('myvuex')) || store.state; store.replaceState(local); store.subscribe((mutations, state) => { const newLocal = _.cloneDeep(state); sessionStorage.setItem('myvuex', JSON.stringify(newLocal)); }); }; }2、使用
export default new Vuex.Store({ modules: { ... }, strict: true, plugins: process.NODE_ENV === 'production' "_blank" href="https://www.npmjs.com/package/vuex-persistedstate">. vuex-persistedstate
2. vuex-persist
总结
以上所述是小编给大家介绍的vuex 中插件的编写案例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
最新资源
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+C
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨
- 何耀珊.2006-收获【华纳】【WAV+CUE】
- 梁汉文1994-壹精选[华星][WAV+CUE]