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()
相关推荐
JELEE.2 小时前
Django登录注册完整代码(图片、邮箱验证、加密)
前端·javascript·后端·python·django·bootstrap·jquery
毕设十刻8 小时前
基于Vue的学分预警系统98k51(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
2501_915909068 小时前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
牧杉-惊蛰9 小时前
纯flex布局来写瀑布流
前端·javascript·css
2501_9151063210 小时前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview
王同学要变强11 小时前
【深入学习Vue丨第二篇】构建动态Web应用的基础
前端·vue.js·学习
社恐的下水道蟑螂11 小时前
从字符串到像素:深度解析 HTML/CSS/JS 的页面渲染全过程
javascript·css·html
程序定小飞11 小时前
基于springboot的web的音乐网站开发与设计
java·前端·数据库·vue.js·spring boot·后端·spring
武昌库里写JAVA12 小时前
element-ui 2.x 及 vxe-table 2.x 使用 css 定制主题
java·vue.js·spring boot·sql·学习
行走的陀螺仪12 小时前
uni-app + Vue3 实现折叠文本(超出省略 + 展开收起)
前端·javascript·css·uni-app·vue3