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