logo头像
Snippet 博客主题

vuex注意事项

本文于1130天之前发表,文中内容可能已经过时。

注意事项

  • 页面刷新的时候store清空,可以使用vuex-persistedstate

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // 简单配置如下
    const vuexPersisted = new createPersistedState({
    key: 'myVuex',
    storage: window.localStorage,
    reducer: state => ({
    // num: state.num
    }),
    filter: mutation => (
    'ADD' === mutation.type
    )
    });
  • 路由动态加载store时候主要注意动态加载的模块的state需要是一个function,要不然会注销该模块的时候会发现注销不了

如何写插件

1
2
3
4
5
6
7
8
9
10
11
12
const myPlugin = store => {
// 当 store 初始化后调用
store.subscribe((mutation, state) => {
// 每次 mutation 之后调用
// mutation 的格式为 { type, payload }
})
}
const store = new Vuex.Store({
// ...
plugins: [myPlugin]
})