uniapp 开发App 使用Vuex持久化存储兼容性最好的方案

首先需要安装依赖:

npm install vuex-persistedstate

直接上代码:

import Vue from 'vue'
import Vuex from 'vuex'
import baseStore from './baseStore.js'
import userStore from './userStore.js'
import getters from './getters'
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    baseStore,
	userStore
  },
  getters,
  plugins: [
	createPersistedState({
		paths: ['userStore'],
		storage: { // 存储方式定义  
			getItem: (key) => uni.getStorageSync(key), // 获取  
			setItem: (key, value) => uni.setStorageSync(key, value), // 存储  
			removeItem: (key) => uni.removeStorageSync(key) // 删除  
		}
	})
  ]
})

export default store

你可能感兴趣的:(uni-app,javascript,前端)