在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,
  },
)
相关推荐
2501_916007471 天前
苹果手机iOS应用管理全指南与隐藏功能详解
android·ios·智能手机·小程序·uni-app·iphone·webview
2501_915106321 天前
全面理解 iOS 帧率,构建从渲染到系统行为的多工具协同流畅度分析体系
android·ios·小程序·https·uni-app·iphone·webview
2501_916008891 天前
iOS 能耗检测的工程化方法,构建多工具协同的电量分析与性能能效体系
android·ios·小程序·https·uni-app·iphone·webview
济南壹软网络科技有限公司1 天前
综合社交服务平台的技术架构与实践:构建高可用、多端覆盖的互动生态
uni-app·php·开源源码·陪玩陪聊h5
2501_915921432 天前
重新理解 iOS 的 Bundle Id 从创建、管理到协作的工程策略
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
当 altool 退出历史舞台,iOS 上传链路的演变与替代方案的工程实践
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张2 天前
Transporter 的局限与替代路径,iOS 上传流程在多平台团队中的演进
android·ios·小程序·https·uni-app·iphone·webview
lwprain2 天前
uniapp使用uniview-plus性能问题处理
uni-app
00后程序员张2 天前
Python 抓包工具全面解析,从网络监听、协议解析到底层数据流捕获的多层调试方案
开发语言·网络·python·ios·小程序·uni-app·iphone
AH_HH2 天前
UniApp H5 代理失效的终极替代方案
uni-app