微信小程序使用pinia-plugin-persistedstate报错找不到localstorage

因为微信小程序种是没有localstorage.setItem的方法的。所以要再使用持久化之后改成一下存储方式

persist.ts

复制代码
import {PersistedStateOptions} from "pinia-plugin-persistedstate";

/**
 * @description pinia 持久化参数配置
 * @param {String} key 存储到持久化的 name
 * @param {Array} paths 需要持久化的 state name
 * @return persist
 * */
const piniaPersistConfig = (key: string, paths?: string[]) => {
    const persist: PersistedStateOptions = {
        key,
        storage: {
            setItem(key, value) {
                uni.setStorageSync(key, value) // [!code warning]
            },
            getItem(key) {
                return uni.getStorageSync(key) // [!code warning]
            },
        },
        // storage: sessionStorage,
        paths
    };
    return persist;
};

export default piniaPersistConfig;

然后在 定义缓存的内容就可以啦

复制代码
import { defineStore } from 'pinia'
import piniaPersistConfig from "./persist";//上面写的哪个文件
复制代码
export interface State {
  userInfo: any;
  globalData: any;
  timerLock: boolean;
  showLoading:boolean;
}
// @ts-ignore
export const useCommonStore = defineStore('pack-store', {
  persist: piniaPersistConfig("pack-store"),
  state: (): State => ({
    userInfo: {},
    globalData: {},
    timerLock: false, //是否关闭合成定时器
    showLoading:false,//loading是否显示
  }),
  getters: {},
  actions: {
    setuserInfo(state: State, data: any){
      this.userInfo = data;
    },
    setGlobalData(data: any){
      this.globalData = Object.assign({}, this.globalData, data);
      console.log("state.globalData", this.globalData);
    },
    logout(){
      uni.removeStorageSync("userInfo");
      uni.removeStorageSync("token");
      uni.removeStorageSync("openInfo");
      uni.clearStorage();
      uni.clearStorageSync();
      this.userInfo = {};
      this.timerLock = true;
    },
    setTimerLock (data: boolean){
      this.timerLock = data;
    },
    setChangeLoading(data:boolean){
      console.log("changeLoading",data)
      this.showLoading = data;
    },
  },
});
相关推荐
2501_933907219 小时前
宁波小程序开发服务与技术团队专业支持
科技·微信小程序·小程序
sheji341618 小时前
【开题答辩全过程】以 基于微信小程序的少儿编程学习平台为例,包含答辩的问题和答案
学习·微信小程序·小程序
const_qiu20 小时前
微信小程序自动化测试100%通过率实践
微信小程序·小程序
Greg_Zhong1 天前
小程序中登出/切换/重置页面状态的使用
小程序
一字白首1 天前
微信小程序进阶实战:从 UI 组件库到全局状态管理全解DAY05
ui·微信小程序·小程序
海石2 天前
微信小程序开发02:原始人也能看懂的着色器与视频处理
前端·微信小程序·视频编码
li9056632802 天前
hanzi-writer-miniprogram Path2D问题以及Bug修复
微信小程序·bug
土土哥V_araolin2 天前
双迪大健康新零售系统开发要点
小程序·个人开发·零售
2501_915909062 天前
不用越狱就看不到 iOS App 内部文件?使用 Keymob 查看和导出应用数据目录
android·ios·小程序·https·uni-app·iphone·webview
CHU7290352 天前
扭蛋机盲盒小程序前端功能设计及核心玩法介绍
前端·小程序