欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3中pinia的使用與持久化處理詳解

 更新時間:2023年07月28日 14:53:44   作者:不禿大師  
Pinia?是一個基于?Vue?3?的狀態(tài)管理庫,可以更好地支持?TypeScript?和更靈活的狀態(tài)管理方式,本文主要介紹了pinia的使用與持久化處理,需要的可以參考一下

簡單記錄一下學(xué)習(xí)pinia的過程

1.Pinia是什么?

Pinia 是一個基于 Vue 3 的狀態(tài)管理庫。 與 Vue 2 中的 Vuex 不同,Pinia 使用了 Vue 3 中的 Composition API,因此可以更好地支持 TypeScript 和更靈活的狀態(tài)管理方式。

2.Pinia有什么特點?

  • 簡單并且易于使用,它的 API 設(shè)計是針對 Composition API 的,因此可以方便地使用 Vue 3 的新特性。
  • 支持 TypeScript,并且提供了強類型的定義,可以在編譯時捕獲錯誤。
  • 支持插件機制,可以輕松地擴展它的功能。
  • 支持多個 store 實例,每個 store 實例都可以擁有自己的狀態(tài)和行為。
  • 支持持久化存儲,可以將 store 中的數(shù)據(jù)保存在本地存儲中,以便在頁面刷新后仍然可以訪問。

服用方法

1.安裝 pinia

bashyarn add pinia#或者使用 npmnpm install pinia

2.在 main.ts/js文件里面進行配置

import { createApp } from 'vue'
++ import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import './assets/main.css'
const app = createApp(App)
++ const pinia = createPinia()
++ app.use(pinia)
app.use(router)
app.mount('#app')

在main.ts/js文件內(nèi)引入Pinia的createPinia()函數(shù)

import { createPinia } from 'pinia'

并且使用createApp(App).use()傳遞給應(yīng)用。

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

3.創(chuàng)建 Pinia 存儲 在你的應(yīng)用程序中,你需要創(chuàng)建一個 Pinia 存儲來管理你的狀態(tài)。你可以使用 createPinia() 函數(shù)來創(chuàng)建一個 Pinia 實例

在src目錄下新建/stores/counter.js 文件.

import { ref, computed } from "vue";
import { defineStore } from "pinia";
// 選項式寫法
// export const useCounterStore = defineStore(
//   "counter",
//   {
//     state: () => ({ count: 0 }),
//     // state: () => {
//     //   return { count: 0 }
//     // },
//     actions: {
//       increment() {
//         this.count++;
//       },
//     },
//   }
// );
// 組合式寫法 
export const useCounterStore = defineStore(
  "counter",
  () => {
    const count = ref(0);
    const increment =()=> {
      count.value++;
    }
    return {
      count,
      increment
    };
  },
  {
  //...更多配置
  }
);

由于Pinia支持Composition API 所以平時會用組合式寫法。 在上面代碼中defineStore有三個參數(shù):

  • 第一個參數(shù):storeName 類型:String 描述:store的名稱。標識該store的唯一性
  • 第二個參數(shù):setup 類型: () => StoreDefinition<State, Getters, Actions, StoreOptions>描述:類似Vue組件中setup()函數(shù)
  • 第三個參數(shù):storestoreOptions(可選) 類型:StoreOptions 描述:一個對象,包含一些 store 的選項

簡單來說 第一個參數(shù)像是一個id 給某個store綁定上,給這個倉庫唯一化。 第二個參數(shù)規(guī)定了倉庫里面的初始值以及變化值。第三個參數(shù) 是個配置選項,例如 persist(是否啟用持久化存儲)、devtools(是否啟用開發(fā)工具)等。 最后使用export 拋出useCounterStore 方法

4.Pinia的使用 在組件中使用Pinia

<script setup>
++ import { useCounterStore } from '@/stores/counter'
++ const counter = useCounterStore()
++ const clickHanlde=()=>{
++  counter.increment()
++ }
</script>
<template>
  <!-- 直接從 store 中訪問 state -->
++  <div>Current Count: {{ counter.count }}</div>
++  <button @click="clickHanlde">加1</button>
</template>
<style scoped>
</style>

從剛才定義的counter.js文件里引入useCounterStore()方法 打印了一下 發(fā)現(xiàn)控制臺可以正常拿到響應(yīng)式數(shù)據(jù) 

并且定義一個變量存儲。同時定義一個點擊事件的函數(shù)

import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()
const clickHanlde=()=>{
  counter.increment()
}

在這個 clickHandle函數(shù)里面 使用counter中我們定義的action 叫做"increment"的方法

使用 template模板寫頁面

<template>
  <!-- 直接從 store 中訪問 state -->
  <div>Current Count: {{ counter.count }}</div>
  <button @click="clickHanlde">增加</button>
</template>

在模板里面使用{{ counter.xxx }}可以正常取到我們放入pinia的數(shù)據(jù) 此時 當點擊按鈕“加1”時 store里面的count會在原來的基礎(chǔ)上++

我們發(fā)現(xiàn)此時count 已經(jīng)增加了

此時刷新瀏覽器,發(fā)現(xiàn)count還是為0,瀏覽器并沒有替我們將count 放在 儲存空間內(nèi)。

解決方案:由于pinia里沒有自帶的持久化存儲,所以我們需要使用要持久化存儲的插件 pinia-plugin-persistedstate npm地址:pinia-plugin-persistedstate 文檔入口:pinia-plugin-persistedstate

1.安裝 pinia-plugin-persistedstate 插件

npm i pinia-plugin-persistedstate
#或者使用 npm
yarn add pinia-plugin-persistedstate

2.安裝完成后 需要在main.ts/js文件內(nèi)進行配置

import { createApp } from 'vue'
import { createPinia } from 'pinia'
++ import piniaPluginPersistedstate  from 'pinia-plugin-persistedstate'
import App from './App.vue'
import router from './router'
import './assets/main.css'
const app = createApp(App)
const pinia = createPinia()
++ pinia.use(piniaPluginPersistedstate)
app.use(pinia)
app.use(router)
app.mount('#app')

引入了piniaPluginPersistedstate 并且使用app.use()傳遞給了應(yīng)用程序。

3.最后再store里面添加配置選項

import { ref, computed } from "vue";
import { defineStore } from "pinia";
// 選項式寫法
// export const useCounterStore = defineStore(
//   "counter",
//   {
//     state: () => ({ count: 0 }),
//     // state: () => {
//     //   return { count: 0 }
//     // },
//     // 也可以這樣定義
//     // state: () => ({ count: 0 })
++//     persist: true,
//     actions: {
//       increment() {
//         this.count++;
//       },
//     },
//   }
// );
// 組合式寫法 
export const useCounterStore = defineStore(
  "counter",
  () => {
    const count = ref(0);
    const increment =()=> {
      count.value++;
    }
    return {
      count,
      increment
    };
  },
  {
++    persist: true,
  },
);

也就是傳說中defineStore的第三個參數(shù)里面的配置項

此時pinia已經(jīng)可以正常使用替我們存儲數(shù)據(jù) 并且也完成了持久化的配置

以上就是Vue3中pinia的使用與持久化處理詳解的詳細內(nèi)容,更多關(guān)于Vue3 pinia的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Vue登錄頁面的動態(tài)粒子背景插件實現(xiàn)

    Vue登錄頁面的動態(tài)粒子背景插件實現(xiàn)

    本文主要介紹了Vue登錄頁面的動態(tài)粒子背景插件實現(xiàn),將登錄組件背景設(shè)置為 "粒子背景",具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • vue實現(xiàn)滾動條到頂部或者到指定位置

    vue實現(xiàn)滾動條到頂部或者到指定位置

    這篇文章主要介紹了vue實現(xiàn)滾動條到頂部或者到指定位置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 使用vite創(chuàng)建vue3之vite.config.js的配置方式

    使用vite創(chuàng)建vue3之vite.config.js的配置方式

    這篇文章主要介紹了使用vite創(chuàng)建vue3之vite.config.js的配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Vue生產(chǎn)環(huán)境如何自動屏蔽console

    Vue生產(chǎn)環(huán)境如何自動屏蔽console

    這篇文章主要介紹了Vue生產(chǎn)環(huán)境如何自動屏蔽console問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Vuex和前端緩存的整合策略詳解

    Vuex和前端緩存的整合策略詳解

    這篇文章主要給大家介紹了Vuex和前端緩存的整合策略的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編一起來看看吧。
    2017-05-05
  • Vue中動態(tài)添加ref的方法詳解

    Vue中動態(tài)添加ref的方法詳解

    在Vue.js項目中,ref是一個非常有用的功能,它可以用來獲取DOM元素或子組件的實例引用,通過ref,我們可以在父組件中直接操作子組件的方法和屬性,或者對DOM元素進行直接操作,本文將詳細介紹如何在Vue中動態(tài)添加ref,并通過多個具體的代碼示例來幫助讀者理解其實現(xiàn)過程
    2024-10-10
  • 通用vue組件化登錄頁面實例代碼

    通用vue組件化登錄頁面實例代碼

    這篇文章主要給大家介紹了關(guān)于通用vue組件化登錄頁面的相關(guān)資料,文中通過圖文以及實例代碼將解決的辦法介紹的非常詳細,對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-08-08
  • Vue如何實現(xiàn)利用vuex永久儲存數(shù)據(jù)

    Vue如何實現(xiàn)利用vuex永久儲存數(shù)據(jù)

    這篇文章主要介紹了Vue如何實現(xiàn)利用vuex永久儲存數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 基于Vue2的獨立構(gòu)建與運行時構(gòu)建的差別(詳解)

    基于Vue2的獨立構(gòu)建與運行時構(gòu)建的差別(詳解)

    下面小編就為大家分享一篇基于Vue2的獨立構(gòu)建與運行時構(gòu)建的差別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • VUE引入DataV報錯解決實戰(zhàn)記錄

    VUE引入DataV報錯解決實戰(zhàn)記錄

    在使用vue開發(fā)大屏?xí)r,發(fā)現(xiàn)了一個很好用的可視化組件庫DataV,下面這篇文章主要給大家介紹了關(guān)于VUE引入DataV報錯解決的實戰(zhàn)記錄,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-04-04

最新評論