uniapp 使用pinpa 持续化更新

安装依赖
   npm i pinia
   npm i pinia-plugin-persistedstate

新建 index.ts

 

import { createPinia } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate' // 数据持久化

const store = createPinia()
store.use(
  createPersistedState({
    storage: {
      getItem: uni.getStorageSync,
      setItem: uni.setStorageSync,
    },
  }),
)

export default store


export * from './user'

新建 uesr.ts
 

import { defineStore } from "pinia";
import { reactive, ref } from "vue";
export const useUserStore = defineStore(
  "user",
  () => {
    const data = ref(999);
    const cktjia = () => {
      data.value++;
    };return {
      data,
      cktjia
    };
  },
  {
    persist: true, // 开启后对 state 的数据读写都将持久化
  }
);

min.ts 文件

 

import store from './store'


 app.use(store)

 

posted @ 2024-11-18 13:09  心动12138  阅读(49)  评论(0编辑  收藏  举报