uni-app vuex的数据持久化

大致的有两种的写法

1.首先 安装

npm install --save vuex-p

2.配置使用

方法一:

import createPersistedState from 'vuex-persistedstate'

const stockpile= new createPersistedState({
    storage: {
        getItem: key => uni.getStorageSync(key),
        setItem: (key, value) => uni.setStorageSync(key, value),
        removeItem: key => uni.removeStorageSync(key)
    }
})

const store = new Vuex.Store({
    plugins:[stockpile]
})

方法二:

import persistedState from 'vuex-persistedstate'
export default new Vuex.Store({
    // ...
    plugins: [persistedState()]
})
import persistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'

export default new Vuex.Store({
  // ...
  plugins: [
    persistedState({
      storage: {
        getItem: key => Cookies.get(key),
        setItem: (key, value) => Cookies.set(key, value, { expires: 7 }),
        removeItem: key => Cookies.remove(key)
      }
    })
  ]
})

猜你喜欢

转载自blog.csdn.net/m0_63856057/article/details/121533649