vuex-persistedstate:可以自动保存 vuex 中的数据到 localStorage,并且在下次开始的时候自动读取这个数据到对应的 state 中
下载依赖
npm install --save vuex-persist
使用方法
store文件内引入
必须注册 plugins
import craetePersistedstate from 'vuex-persistedstate'
const store = createStore({
......
plugins: [
craetePersistedstate({
// 指定保存到 localStorage中的key
key: 'imooc-front',
// 需要保存的模块
paths: ['category']
})
]
})
export default store