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 狀態
  • 跟蹤狀態變更來源

科技分類資訊推薦

梁文鋒署名,DeepSeek再發論文 - 天天要聞

梁文鋒署名,DeepSeek再發論文

近日DeepSeek團隊發表新論文論文以DeepSeek-V3為核心案例披露其在AI硬件架構與模型設計方面的關鍵創新為實現具有成本效益的大規模訓練和推理提供思路論文顯示DeepSeek創始人兼CEO梁文鋒也是參與者之一DeepSeek論文《
天地在線隆福寺「全時空間數字藝術中心」盛大開業,開啟數字娛樂與文化融合新體驗 - 天天要聞

天地在線隆福寺「全時空間數字藝術中心」盛大開業,開啟數字娛樂與文化融合新體驗

5月16日,由天地在線(股票代碼:002995)傾力打造的「全時空間數字藝術中心」首家直營旗艦店在北京隆福寺正式開業。這座佔地超1000平方米的沉浸式VR體驗空間,以「科技+文化+娛樂」為核心,將千年文脈與前沿數字技術深度融合,為京城消費者帶來一場跨越時空的奇幻之旅。 文化地標煥新:百年隆福寺的“數字蝶變” 隆福寺,...
“疑美論”成賴清德包袱,民調滿意度創新低 - 天天要聞

“疑美論”成賴清德包袱,民調滿意度創新低

據中評網報道,台灣地區領導人賴清德520執政將滿一周年,施政內外交迫,尤其是美國對等關稅、半導體關稅都尚未定案,充滿不確定性,台行政機構負責人卓榮泰16日與媒體茶敘出動三大將說明對美談判進度,不難看....
async/await 錯誤處理的陷阱:生產環境踩過的5個坑 - 天天要聞

async/await 錯誤處理的陷阱:生產環境踩過的5個坑

async/await讓JavaScript異步編程變得更加直觀和優雅。然而,在處理錯誤時,這種語法糖也隱藏了許多容易被忽視的陷阱。作為一名經歷過無數深夜緊急修復的開發者,分享下生產環境中親身經歷的5個async/await錯誤處理陷阱,以
su7事故的內部會議,雷軍:“沒想到對小米打擊如此大” - 天天要聞

su7事故的內部會議,雷軍:“沒想到對小米打擊如此大”

文|十五在閱讀此文之前,辛苦您點擊一下“關注”,既方便您進行討論和分享,又能給您帶來不一樣的參與感,感謝您的支持!今年的小米因為一場意外走向了前所未有的行業寒冬,一瞬間無數問題指向小米。三條人命的逝去,死者親人的痛訴,所有的問題都指向了事故