vuex持久化

1、使用场景 vuex刷新之后数据会消失
2、使用方法
(1)结构1
image
image

点击查看代码
import { createStore } from "vuex"
import state from './state'
import mutations from './mutations'
import actions from './actions'
import * as getters from './getters'
import persistedState from 'vuex-persistedstate' // vuex持久化插件

const Store = createStore({
  state,
  mutations,
  actions,
  getters,
  plugins: [persistedState({
    storage: window.sessionStorage,
    reducer: (val) =>{ 
	// 存储state中的userInfo
      return { userInfo: val['userInfo']} }
  })],
})

export default Store
(2)结构2

image

点击查看代码
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'
import persistedState from 'vuex-persistedstate' // vuex持久化插件

Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    app,
    settings,
    user
  },
  getters,
  plugins: [persistedState({
    storage: window.sessionStorage,
    key:"vuex" // app sttings user 都存在session中了
  })],
})

export default store
posted @ 2023-04-28 14:36  你的眼里有星星  阅读(125)  评论(0编辑  收藏  举报