uniapp微信小程序开发踩坑日记:Pinia持久化报错Cannot read property ‘localStorage‘ of undefined

插件默认使用 localStorage 实现持久化,小程序端不兼容,需要替换持久化 API

import { defineStore } from 'pinia'
 

export const useCommonStore = defineStore('pack-store', {
  state: (): State => ({
    wwInfo: {},
    globalData: {},
    timerLock: false, //是否关闭合成定时器
  }),
  getters: {},
  actions: {
    setWwInfo: (state: State, data: any) => {
      this.wwInfo = data;//注意这里不用加state。直接this就行
    },
},
  {
  // 配置持久化
  persist: {
    // 调整为兼容多端的API
    storage: {
      setItem(key, value) {
        uni.setStorageSync(key, value) 
      },
      getItem(key) {
        return uni.getStorageSync(key) 
      },
    },
  },
},
)

你可能感兴趣的:(uni-app,微信小程序,小程序)