在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,
  },
)
相关推荐
codingWhat1 天前
小程序里「嵌」H5:一套完整可落地的 WebView 集成方案
前端·uni-app·webview
小时前端2 天前
微信小程序选不了本地文件?用 web-view + H5 一招搞定
前端·微信小程序·uni-app
Mr_li3 天前
给 Vue 开发者的 uni-app 快速指南
vue.js·uni-app
anyup3 天前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
Mintopia4 天前
Vue3 项目如何迁移到 uni-app x:从纯 Web 到多端应用的系统指南
uni-app
Mintopia4 天前
uni-app x 发展前景技术分析:跨端统一的新阶段?
uni-app
不爱说话郭德纲5 天前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
HashTang6 天前
【AI 编程实战】第 12 篇:从 0 到 1 的回顾 - 项目总结与 AI 协作心得
前端·uni-app·ai编程
JunjunZ6 天前
uniapp 文件预览:从文件流到多格式预览的完整实现
前端·uni-app
郑州光合科技余经理7 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php