uniapp使用pinia存储(vue3+ts)

1.安装pinia和持久化插件

c 复制代码
npm install pinia
c 复制代码
npm install pinia-plugin-persistedstate

2.main.ts 文件中配置该插件

c 复制代码
import { createSSRApp } from "vue";
import App from "./App.vue";
import { createPinia } from 'pinia';
import piniaPersistedState from 'pinia-plugin-persistedstate';
import TopNavigation from "./compontens/topNavigation.vue";

export function createApp() {
  const pinia = createPinia();
  // 配置持久化插件
  pinia.use(piniaPersistedState);
  const app = createSSRApp(App);
  app.component('top-nav', TopNavigation)
  app.use(pinia)
  return {
    app,
  };
}

3.新建文件src/store/logintoken.ts

c 复制代码
import { defineStore } from 'pinia';
import { ref } from "vue";
export const useLogintokenStore = defineStore('logintoken', () => {
    // 会员信息
    const profile = ref()

    //定义一个函数来保存信息,本地缓存
    const setProfile = (val: any) => {
        profile.value = val
    }
    //定义一个函数来修改清理会员信息,清除全部存储
    const clearProfile = () => profile.value = undefined
    // 定义一个函数来修改或新增 profile 里面的值
    const setProfileValue=(key: string, value: any)=> {
        // 直接通过键名设置或更新值,如果键名不存在则新增
        profile.value[key] = value;
    }
    // 定义一个函数来修改 profile 里面的值
    const updateProfileValue = (key: string, value: any) => {
        if (key in profile.value) {
            // 如果键名存在于 profile 中,则更新其值
            profile.value[key] = value;
            console.log(profile)
        } else {
            console.warn(`Key "${key}" does not exist in profile.`);
        }
    }
    // 定义一个函数来删除 profile 里面的值
    const deleteProfileValue = (key: string) => {
        if (key in profile.value) {
            // 如果键名存在于 profile 中,则删除该属性
            delete profile.value[key];
            console.log(profile)
        } else {
            console.warn(`Key "${key}" does not exist in profile.`);
        }
    }

    //记得 return
    return { profile, setProfile, clearProfile, updateProfileValue, deleteProfileValue,setProfileValue }
},
    {
        persist: {
            storage: {
                getItem(key) {
                    return uni.getStorageSync(key)
                },
                setItem(key, value) {
                    uni.setStorageSync(key, value)
                },
            }
        }
    });

4.使用

c 复制代码
import { useLogintokenStore } from '@/stores/logintoken'

let msg = {
 idserveUserId: res.result.userId,
 isFollowedWeChat: res.result.isFollowedWeChat,
 openId: res.result.openId,
          }
const memberStore=useLogintokenStore()
 memberStore.setProfile(msg)

清除

c 复制代码
const memberStore=useLogintokenStore()
 memberStore.clearProfile()
相关推荐
谢尔登1 天前
原型理解从入门到精通
开发语言·javascript·原型模式
粥里有勺糖1 天前
视野修炼-技术周刊第127期 | Valdi
前端·javascript·github
码上成长1 天前
Vue Router 3 升级 4:写法、坑点、兼容一次讲透
前端·javascript·vue.js
星光一影1 天前
陪诊陪检系统源码,陪诊小程序,陪诊APP,陪诊服务,家政上门系统,居家护理陪护源码
mysql·小程序·uni-app·php
qq_398586541 天前
浏览器中内嵌一个浏览器
前端·javascript·css·css3
abiao19811 天前
如何在 VSCode 中创建 Vue 项目
ide·vue.js·vscode
西西西西胡萝卜鸡1 天前
虚拟列表(Virtual List)组件实现与优化铁臂猿版(简易版)
前端·vue.js
宇余1 天前
Unibest:新一代uni-app工程化最佳实践指南
前端·vue.js
Qlittleboy1 天前
uniapp里 rich-text 里的img图片如何控制最大宽度
uni-app
阿奇__1 天前
uniapp h5 app 小程序获取当前定位
小程序·uni-app