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()
相关推荐
LXA080920 小时前
Vue 3中使用JSX
前端·javascript·vue.js
执携20 小时前
Vue Router (历史模式)
前端·javascript·vue.js
是梦终空20 小时前
vue下载依赖报错npm ERR node-sass@4.14.1 postinstall: `node scripts/build.js`的解决方法
javascript·npm·vue·node-sass·vue依赖
依米_20 小时前
一文带你剖析 Promise.then all 实现原理,状态机、发布订阅模式完美实现异步编程
javascript·设计模式
G018_star sky♬20 小时前
使用npm运行js脚本覆盖vue依赖包
javascript·vue.js
宇余20 小时前
从 useState 到 URLState:前端状态管理的另一种思路
前端·vue.js
T___T21 小时前
Ajax 数据请求详解与实战
javascript·面试
冴羽21 小时前
能让 GitHub 删除泄露的苹果源码还有 8000 多个相关仓库的 DMCA 是什么?
前端·javascript·react.js
悟能不能悟21 小时前
jsp怎么拿到url参数
java·前端·javascript
程序猿_极客21 小时前
JavaScript 的 Web APIs 入门到实战全总结(day7):从数据处理到交互落地的全链路实战(附实战案例代码)
开发语言·前端·javascript·交互·web apis 入门到实战