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()
相关推荐
quitv25 分钟前
react脚手架配置别名
前端·javascript·react.js
化作繁星36 分钟前
在 Vue 3 中,如何缓存和复用动态组件
前端·vue.js·缓存
一只小姜丝3321 小时前
解决各大浏览器中http地址无权限调用麦克风摄像头问题
网络·vue.js·网络协议·http
Gazer_S2 小时前
【现代前端框架中本地图片资源的处理方案】
前端·javascript·chrome·缓存·前端框架
林的快手4 小时前
CSS默认样式
前端·css·vue.js·chrome·css3
贺今宵4 小时前
通过$attrs传递的未指定名称的modelValue值在子组件中修改
前端·javascript
lifire_H8 小时前
Canvas在视频应用中的技术解析
前端·javascript·音视频
林涧泣8 小时前
【Uniapp-Vue3】开发userStore用户所需的相关操作
前端·vue.js·uni-app
皓月当空hy10 小时前
vue3中测试:单元测试、组件测试、端到端测试
vue.js
十八朵郁金香10 小时前
深入理解 JavaScript 中的 this 指向
开发语言·前端·javascript