Pinia 最新发布的 6 个插件,效率提升 300%!

2025年05月17日09:13:06 科技 1728

Pinia 最新发布的 6 个插件,效率提升 300%! - 天天要闻

Pinia 作为 Vue 官方推荐的状态管理库,其插件机制能有效扩展核心功能,覆盖 数据持久化、性能优化、开发调试、类型安全 等常见需求。

一、数据持久化插件:pinia-plugin-persistedstate

痛点: 页面刷新后状态丢失

npm install pinia-plugin-persistedstate
// main.ts
import { createPinia } from'pinia'
import piniaPluginPersistedstate from'pinia-plugin-persistedstate'

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)

// store 配置
exportconst useUserStore = defineStore('user', {
  state: () => ({ token: '' }),
  persist: {
    storage: sessionStorage, // 指定存储方式
    paths: ['token'] // 选择持久化字段
  }
})

适用场景:

  • 保持用户登录状态
  • 记住表格筛选条件
  • 保存用户主题偏好

二、状态重置插件:pinia-plugin-reset

痛点: 手动重置复杂状态容易遗漏

import { createReset } from 'pinia-plugin-reset'

// 初始化
pinia.use(createReset())

// 使用示例
const store = useUserStore()
store.$reset() // 一键重置到初始状态

**适用场景:

  • 用户退出登录时清理数据
  • 表单提交后重置
  • 测试用例的初始状态设置

三、防抖操作插件:@pinia/plugin-debounce

顾名思义,防抖触发 pinia 更新,减少更新频率

import { debounce } from 'ts-debounce'
import { PiniaDebounce } from '@pinia/plugin-debounce'

// Pass the plugin to your application's pinia plugin
pinia.use(PiniaDebounce(debounce))
defineStore(
  'id',
() => {
    // ...the store

    return { someSearch }
  },
  {
    debounce: {
      // 防抖
      someSearch: 300,
    },
  }
)

四、映射操作插件:pinia-orm

通过映射关系来操作 pinia

Pinia 最新发布的 6 个插件,效率提升 300%! - 天天要闻

import { createPinia } from 'pinia'
import { createORM } from 'pinia-orm'
const pinia = createPinia().use(createORM())
// User Model

import { Model } from'pinia-orm'

exportdefaultclass User extends Model {
// entity is a required property for all models.
static entity = 'users'

// List of all fields (schema) of the post model. `this.string()` declares
// a string field type with a default value as the first argument.
// `this.uid()` declares a unique id if none provided.
static fields () {
    return {
      id: this.uid(),
      name: this.string(''),
      email: this.string('')
    }
  }
// For typescript support of the field include also the next lines
declare id: string
declare name: string
declare email: string
}

五、撤销/重置插件:pinia-undo

pnpm add pinia pinia-undo
import { PiniaUndo } from 'pinia-undo'

const pinia = createPinia()
pinia.use(PiniaUndo)
const useCounterStore = defineStore({
  id: 'counter',
  state: () => ({
    count: 10,
  }),
  actions: {
    increment() {
      this.count++
    },
  },
})

const counterStore = useCounterStore()

// undo/redo have no effect if we're at the
// beginning/end of the stack
counterStore.undo() // { count: 10 }
counterStore.redo() // { count: 10 }

counterStore.increment() // { count: 11 }
counterStore.increment() // { count: 12 }

counterStore.undo() // { count: 11 }
counterStore.undo() // { count: 10 }
counterStore.undo() // { count: 10 }

counterStore.redo() // { count: 11 }

六、SSR 支持:@pinia/nuxt

npm install @pinia/nuxt
// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@pinia/nuxt']
})

// 自动处理:  
// - 服务端数据预取  
// - 客户端状态同步  
// - 避免 hydration 错误

七、开发调试插件:vue-devtools 增强

内置支持: 通过浏览器插件直接查看

调试技巧:

  • 时间旅行调试(状态回滚)
  • 直接修改 store 状态
  • 跟踪状态变更来源

科技分类资讯推荐

获巨额云计算合同!甲骨文股价创新高 - 天天要闻

获巨额云计算合同!甲骨文股价创新高

智通财经APP获悉,甲骨文(ORCL.US)表示,该公司已签署一项年收入高达300亿美元的单一云计算服务合同,这一金额已超过其当前整个云基础设施业务的规模。受该消息推动,甲骨文股价周一早盘一度上涨8.6%,创下盘中历史新高,收盘涨近4%。今年以来,甲骨文股价已累计上涨约32%。甲骨文在周一提交的一份监管文件中披露,这笔收入...
中金:运营商配合意愿亟待提升 eSIM热度有望重启 - 天天要闻

中金:运营商配合意愿亟待提升 eSIM热度有望重启

智通财经APP获悉,中金发布研报称,根据GSMA Intelligence预测,至2025年底,全球预计将有约10亿eSIM智能手机连接,2030年将增长至69亿,随着消费电子轻薄化、可穿戴设备以及物联网终端漫游需求增加,eSIM的应用有望在2025年加速。但eSIM的应用落地仍面临运营商配合意愿度不高的阻力,支持终端数量有限。eSIM卡具备小尺寸、...
网易腾讯发布暑期限玩日历:未成年人每周限玩3小时 - 天天要闻

网易腾讯发布暑期限玩日历:未成年人每周限玩3小时

随着暑期来临,未成年人网络使用需求显著上升。为防止未成年人沉迷游戏,6月30日,网易游戏、腾讯游戏分别发布了2025年暑期未成年人限玩日历(7月1日-8月31日),两家企业均采取严格时段限制措施,规则基本一致。
“闪灯即推+边推边开”!太原机场开启高效运行新模式 - 天天要闻

“闪灯即推+边推边开”!太原机场开启高效运行新模式

近日,东航MU5273航班在T2站坪顺利完成“闪灯即推+边推边开”新流程操作,这标志着航空器地面高效运行新模式在太原国际机场有限责任公司正式投入应用。资料图 据介绍,太原机场公司二跑道预计在2025年底正式投运。经模拟机测算,航空器地面滑行距离、滑行时间将显著增加。不仅直接冲击航班正常性,也将极大影响机场整体运行...