一文掌握Pinia使用及數(shù)據(jù)持久化存儲(chǔ)超詳細(xì)教程
一.安裝及使用Pinia
1.安裝Pinia兩種方式都可,根據(jù)個(gè)人習(xí)慣來(lái)
npm install pinia yarn add pinia
2.在main.ts 中引入并掛載到根實(shí)例
// src/main.ts import { createApp } from 'vue' import App from './App.vue' import { createPinia } from 'pinia' // 創(chuàng)建Vue應(yīng)用實(shí)例 // 實(shí)例化 Pinia // 以插件形式掛載Pinia實(shí)例 createApp(App).use(createPinia()).mount('#app')
3.src目錄下新建store/study/index.js并寫(xiě)入
Store 是用defineStore()定義的,它的第一個(gè)參數(shù)是一個(gè)獨(dú)一無(wú)二的id,也是必須傳入的,Pinia 將用它來(lái)連接 store 和 devtools。
defineStore()第二個(gè)參數(shù)可接受兩類值:Setup函數(shù)或Options對(duì)象
state 屬性: 用來(lái)存儲(chǔ)全局的狀態(tài)的,這里邊定義的,就可以是為SPA里全局的狀態(tài)了。
getters屬性:用來(lái)監(jiān)視或者說(shuō)是計(jì)算狀態(tài)的變化的,有緩存的功能。
actions屬性:對(duì)state里數(shù)據(jù)變化的業(yè)務(wù)邏輯,需求不同,編寫(xiě)邏輯不同。說(shuō)白了就是修改state全局狀態(tài)數(shù)據(jù)的。
第一種Options式寫(xiě)法:
import { defineStore } from 'pinia' // `defineStore()` 的返回值命名最好使用 store 的名字,同時(shí)以 `use` 開(kāi)頭且以 `Store` 結(jié)尾 export const useStudyStore = defineStore('studyId', { state: () => { return { counter: 0, } }, getters:{}, actions:{} })
在Options式中:Store 的數(shù)據(jù)(data),getters 是 Store 的計(jì)算屬性(computed),而actions則是 Store 的方法(methods)。
第二種Setup式寫(xiě)法:
import { defineStore } from 'pinia' // `defineStore()` 的返回值命名最好使用 store 的名字,同時(shí)以 `use` 開(kāi)頭且以 `Store` 結(jié)尾 export const useStudyStore = defineStore('studyId', ()=>{ const count = ref(0) const name = ref('Ghmin') const computedTest= computed(() => count.value * 99) function int() { count.value++ } return { count, name, computedTest, int} })
在Setup式中:ref()成為state屬性,computed()變成getters,function變成actions
4.使用Store
使用上面兩種方式其中一種后,便可以在組件中使用Store了。
<script setup> import { useStudyStore } from '@/stores/study' const store = useStudyStore(); </script>
二.具體使用及屬性與方法
1.定義數(shù)據(jù)
import { defineStore } from 'pinia' export const useStudyStore = defineStore('studyId', { state: () => { return { name: 'Ghmin', num:0 } }, })
2.組件中使用
<template> <div> <h1>vue組件</h1> {{ name }} </div> </template> <script setup> import { useStudyStore } from '@/stores/study' const store = useStudyStore(); let { name } = store; </script>
注:pinia可以直接修改state數(shù)據(jù),無(wú)需像vuex一樣通過(guò)mutations才可以修改,所以上面的let { name } = store 這種解構(gòu)是不推薦的,因?yàn)樗茐牧隧憫?yīng)性。
而為了從 Store 中提取屬性,同時(shí)保持其響應(yīng)性,這里需要使用storeToRefs(),它將為每個(gè)響應(yīng)性屬性創(chuàng)建引用。當(dāng)你只使用 Store 的狀態(tài)而不調(diào)用任何action時(shí),它會(huì)非常有用。使用方法如下
<template> <div> <h1>vue組件</h1> {{ name }} </div> </template> <script setup> //這里需要先引入 import { storeToRefs } from 'pinia' import { useStudyStore } from '@/stores/study' const store = useStudyStore(); //這樣解構(gòu)的屬性將保持響應(yīng)性 let { name } = storeToRefs(store); // name.value 可以直接修改到Store中存儲(chǔ)的值 </script>
如果有多條數(shù)據(jù)要更新?tīng)顟B(tài),推薦使用$patch方式更新。因?yàn)镻inia的官方網(wǎng)站,已經(jīng)明確表示$ patch的方式是經(jīng)過(guò)優(yōu)化的,會(huì)加快修改速度,對(duì)程序的性能有很大的好處。
<template> <div> <h1>A組件</h1> {{ num}} {{ arr }} <button @click='btn'>按鈕</button> </div> </template> <script setup> import { storeToRefs } from 'pinia' import { useStudyStore } from '@/stores/study' const store = useStudyS let { num,arr } = storeToRefs(store); const btn = ()=>{ //批量更新 store.$patch(state=>{ state.num++; state.arr.push({name:'Ghmin'}); }) } </script>
actions:對(duì)state里數(shù)據(jù)變化的業(yè)務(wù)邏輯,需求不同,編寫(xiě)邏輯不同。說(shuō)白了就是修改state全局狀態(tài)數(shù)據(jù)的。
import { defineStore } from 'pinia' export const useStudyStore = defineStore('studyId', { state: () => { return { num: 0 } }, getters:{}, actions:{ changeNum( val ){ this.num+= val; } } }) <template> <div> <h1>使用actions</h1> {{ num}} <button @click='add'>加99</button> </div> </template> <script setup> import { storeToRefs } from 'pinia' import { useStudyStore } from '@/stores/study' const store = useStudyStore(); let { num} = storeToRefs(store); const add = ()=>{ store.changeNum(10); } </script>
getters:和vuex的getters幾乎類似,用來(lái)監(jiān)視或者說(shuō)是計(jì)算狀態(tài)的變化的,有緩存的功能。
import { defineStore } from 'pinia' export const useStudyStore = defineStore('studyId', { state: () => { return { num: 0, } }, getters:{ numGetters(){ return this.counter + 999; } }, actions:{} }) <template> <div> <h1>getters的使用</h1> {{ num}} {{ numGetters}} </div> </template> <script setup> import { storeToRefs } from 'pinia' import { useStudyStore } from '@/stores/study' const store = useStudyStore(); let { num,numGetters} = storeToRefs(store); </script>
三.數(shù)據(jù)持久化存儲(chǔ)
使用pinia-plugin-persist實(shí)現(xiàn)數(shù)據(jù)持久化存儲(chǔ),具體使用請(qǐng)?zhí)D(zhuǎn)Pinia持久化存儲(chǔ)
http://www.dbjr.com.cn/python/292471hnu.htm
到此這篇關(guān)于快速搞懂Pinia及數(shù)據(jù)持久化存儲(chǔ)(詳細(xì)教程)的文章就介紹到這了,更多相關(guān)Pinia數(shù)據(jù)持久化存儲(chǔ)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入淺析Vue.js中 computed和methods不同機(jī)制
這篇文章給大家介紹了Vue.js中 computed和methods不同機(jī)制,在vue.js中,methods和computed兩種方式來(lái)動(dòng)態(tài)當(dāng)作方法使用,文中還給大家提到了computed和methods的區(qū)別,感興趣的朋友一起看看吧2018-03-03vue項(xiàng)目中圖片選擇路徑位置static或assets的區(qū)別及說(shuō)明
這篇文章主要介紹了vue項(xiàng)目中圖片選擇路徑位置static或assets的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Vue實(shí)現(xiàn)指令式動(dòng)態(tài)追加小球動(dòng)畫(huà)組件的步驟
這篇文章主要介紹了Vue實(shí)現(xiàn)指令式動(dòng)態(tài)追加小球動(dòng)畫(huà)組件的步驟,幫助大家更好的理解和實(shí)用vue,感興趣的朋友可以了解下2020-12-12vue3中使用pinia(大菠蘿)狀態(tài)管理倉(cāng)庫(kù)的項(xiàng)目實(shí)踐
本文主要介紹了vue3中使用pinia(大菠蘿)狀態(tài)管理倉(cāng)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07vue實(shí)現(xiàn)PC端錄音功能的實(shí)例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)PC端錄音功能的實(shí)例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Vue2.0 實(shí)現(xiàn)移動(dòng)端圖片上傳功能
本文主要介紹VUE2.0圖片上傳功能的實(shí)現(xiàn)。原理是通過(guò)js控制和input標(biāo)簽的方式完成這一效果,無(wú)需加載其他組件。具體實(shí)例大家大家參考下本文2018-05-05新版vue-cli模板下本地開(kāi)發(fā)環(huán)境使用node服務(wù)器跨域的方法
這篇文章主要介紹了新版vue-cli模板下本地開(kāi)發(fā)環(huán)境使用node服務(wù)器跨域的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04