在uniapp中使用pinia

引入pinia-plugin-persistedstate实现数据持久化

javascript 复制代码
import { createPinia } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate' // 数据持久化

const store = createPinia()
// 使用持久化插件,适配 UniApp 的存储 API
store.use(
  createPersistedState({
    storage: {
      getItem: (key) => uni.getStorageSync(key),
      setItem: (key, value) => uni.setStorageSync(key, value),
      removeItem: (key) => uni.removeStorageSync(key),
    },
  }),
)

export default store

// 模块统一导出
export * from './user'

核心API

defineStore:

第一个参数是id,

第二个参数可接受两类值:Setup 函数或 Option 对象。

javascript 复制代码
// Option 对象
export const useCounterStore = defineStore('counter', {
  state: () => ({ 
    count: 0, 
    name: 'Eduardo' 
  }),
  
  getters: {
    doubleCount: (state) => state.count * 2,
    greeting: (state) => `Hello, ${state.name}!`,
  },
  
  actions: {
    increment() {
      this.count++
    },
    updateName(newName) {
      this.name = newName
    },
  },
  
  // 持久化配置
  persist: {
    key: 'counter-store', // 自定义存储键名
    paths: ['count'], // 只持久化 count 状态
  },
})
javascript 复制代码
// Setup 函数
import { defineStore } from 'pinia'
import { ref } from 'vue'

const initState = {
  token: '',
  userid: '',
  username: '',
  realname: '',
  welcome: '',
}

export const useUserStore = defineStore(
  'user',
  () => {
  //相当于state
    const userInfo = ref<IUserInfo>({ ...initState })

	//相当于actions
    const setUserInfo = (val: IUserInfo) => {
      if(val?.loginTenantId){
        val.tenantId = val.loginTenantId;
      }
      userInfo.value = val
    }
    const clearUserInfo = () => {
      userInfo.value = { ...initState }
    }
    const getUserInfo = () => {
      return userInfo.value
    }
    const editUserInfo = (options) => {
      userInfo.value = { ...userInfo.value, ...options }
    }
    const setTenant = (tenantId) => {
      userInfo.value.tenantId = tenantId;
    }
    const getTenant = () => {
      return userInfo.value.tenantId;
    }
    const reset = () => {
      userInfo.value = { ...initState }
    }
    
    //相当于getters
    const isLogined = computed(() => !!userInfo.value.token)
    return {
      userInfo,
      setUserInfo,
      getUserInfo,
      clearUserInfo,
      setTenant,
      getTenant,
      isLogined,
      editUserInfo,
      reset,
    }
  },
  {
    // 如果需要持久化就写 true, 不需要持久化就写 false(或者去掉这个配置项)
    persist: true,
  },
)
相关推荐
PedroQue995 小时前
v2.7.1:修复 H5 端返回死循环闪烁问题
前端·uni-app
lhldsg6 小时前
AI零售系统实战指南:从架构设计到落地部署全解析
java·人工智能·小程序·uni-app·零售
RisunJan1 天前
uni-app 面试知识点梳理
面试·职场和发展·uni-app
郑州光合科技余经理2 天前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php
lhldsg3 天前
家校托管互通系统开发实战:从需求分析到部署全指南
小程序·uni-app·需求分析
paopaokaka_luck3 天前
基于springboot3+vue3+uniapp的河南非遗数字图谱小程序(协同过滤算法、数字图谱展示、ECharts 图形化分析)
java·前端·spring boot·学习·小程序·uni-app·echarts
宠友信息4 天前
社区类源码开发实践中的仿小红书系统技术要点分析
java·spring boot·redis·mysql·uni-app·vue·内容运营
郑州光合科技余经理4 天前
本地生活平台搭建:统一订单表与多后台切换怎么拆
java·开发语言·前端·系统架构·uni-app·php·ai编程
lhldsg4 天前
树洞交友系统开发实战:从匿名社交需求到技术落地
java·小程序·uni-app·交友