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 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
速易达网络2 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码
耶啵奶膘2 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
JoJo_Way2 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
视频砖家3 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能
lyj1689973 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
GISer_Jing6 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆6 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试
我在北京coding7 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js