Vue3中Pinia的基本使用筆記
什么是Pinia呢?
Pina開始于大概2019,是一個(gè)狀態(tài)管理的庫(kù),用于跨組件、頁面進(jìn)行狀態(tài)共享(這和Vuex、Redux一樣),用起來像組合式API(Composition API)
Pinia和Vuex的區(qū)別
- PInia的最初是為了探索Vuex的下一次迭代會(huì)是什么樣子,結(jié)合了Vuex核心團(tuán)隊(duì)討論中的許多想法;
- 最終,團(tuán)隊(duì)意識(shí)到Pinia已經(jīng)實(shí)現(xiàn)了Vuex5中大部分內(nèi)容,所以最終決定用Pinia來替代Vuex;
- 與Vuex相比,Pinia提供了一個(gè)更簡(jiǎn)單的API,具有更少的儀式,提供了Composition-API風(fēng)格的API
- 更重要的是,與TypeScript一起使用時(shí)具有可靠的類型推斷支持
與Vuex相比,Pinia很多的優(yōu)勢(shì):
比如mutations不再存在:
- mutations最初是為devtools集成,但這不在是問題
- 他們經(jīng)常認(rèn)為是非常冗長(zhǎng)
更友好的TpeScipt支持,Vuex之前對(duì)Ts的支持很不友好
不在有modules的嵌套結(jié)構(gòu)
- 你可以靈活使用每一個(gè)store,他們是通過扁平化的方式來相互使用的;
不在有命名空間的概念,不在需要記住他們的復(fù)雜關(guān)系
如何使用Pinia
1、安裝Pinia
- yarn add pinia
- npm install pinia
2、創(chuàng)建pinia文件
store文件里index.js
import { createPinia } from 'pinia' const pinia = createPinia() export default pinia
3、main.js導(dǎo)入并引用
import { createApp } from 'vue' import App from './App.vue' import pinia from './stores' createApp(App).use(pinia).mount('#app')
4、pinia的狀態(tài)管理,不同狀態(tài)可以區(qū)分不同文件
//定義關(guān)于counter的store import { defineStore } from ‘pinia' //defineStore 是返回一個(gè)函數(shù) 函數(shù)命名最好有use前綴,根據(jù)函數(shù)來進(jìn)行下一步操作 const useCounter = defineStore('counter',{ state: () => { count:99 } }) export default useCounter
5、調(diào)用pinia,獲取pinia狀態(tài)值,導(dǎo)入Counter.js,獲取Counter.js里面state.count
<template> <div class="home"> <h2>Home View</h2> <h2>count: {{ counterStore.count }}</h2> </div> </template> <script setup> import useCounter from '@/stores/counter'; const counterStore = useCounter() </script> <style scoped> </style>
注意:pinia解構(gòu)出來的state也是可以調(diào)用,但會(huì)失去響應(yīng)式,需要toRef或者pinia自帶storeToRefs
<template> <div class="home"> <h2>Home View</h2> <h2>count: {{ counterStore.count }}</h2> <h2>count: {{ count }}</h2> <button @click="incrementCount">count+1</button> </div> </template> <script setup> import { toRefs } from 'vue' import { storeToRefs } from 'pinia' import useCounter from '@/stores/counter'; const counterStore = useCounter() // const { count } = toRefs(counterStore) const { count } = storeToRefs(counterStore) function incrementCount() { counterStore.count++ } </script> <style scoped> </style>
store的核心部分:state,getter,action
(相當(dāng)于:data、computed、methods)
認(rèn)識(shí)和定義State
state是store的核心部分,因?yàn)閟tore是用來幫助我們管理狀態(tài)
操作State
1.讀取和寫入state:
默認(rèn)情況下,可以通過store實(shí)例訪問狀態(tài)來直接讀取和寫入狀態(tài);
``` const counterStore = useCounter() counterStore.counter++ counterStore.name = 'coderWhy' ```
2.重置State:
可以調(diào)用store上的$reset()方法將狀態(tài)重置到其初始值
const counterStore = useCounter() conterStore.$reset()
3.改變State
- 除了直接用store.counter++修改store,還可以調(diào)用$patch
- 它允許您使用部分‘state’對(duì)象同時(shí)應(yīng)該多個(gè)修改
const counterStore = useCounter() counterStore.$patch({ counter:100, name:'kobe' })
4.替換State
可以通過將其$state屬性設(shè)置為新對(duì)象替換Store的整個(gè)狀態(tài)
conterStore.$state = { counter:1, name:'why' }
認(rèn)識(shí)和定義Getters
Getters相當(dāng)于Store的計(jì)算屬性:
- 它們可用defineStore()中的getters屬性定義
- getters中可以定義接受一個(gè)state作為參數(shù)的函數(shù)
expoer const useCounter = defineStore('counter',{ state: () => { counter:100, firstname:'kobe' }, getters:{ doubleCounter(state){ return state.counter *2 } } })
訪問Store里getters方法
1.訪問當(dāng)前store的getters:
const counterSotre = useCounter() console.log(counterStore.doublCounter)
2.我們可以使用this來訪問當(dāng)前的store實(shí)例中g(shù)etters
expoer const useCounter = defineStore('counter',{ state: () => { counter:100, firstname:'kobe' }, getters:{ doubleCounter(state){ return state.counter *2 } doubleCounterAdd(){ //this指向store return this.doubleCounter +1 } } })
3.訪問其它store的getters
import useUser from ./user const userStore = useUser() expoer const useCounter = defineStore('counter',{ state: () => { counter:100, firstname:'kobe' }, getters:{ //調(diào)用其它Store doubleCounterUser(){ return this.doubleCounter + userStore.umu } } })
4.通過getters可以返回一個(gè)函數(shù),可以傳參數(shù)
expoer const useCounter = defineStore('counter',{ state: () => { counter:100, firstname:'kobe' }, getters:{ //調(diào)用其它Store doubleCounter(state){ return function (is) { return state.id + id } } } })
const StoreConter = useCounter(); //傳參 StoreCounter.doublCounter(111)
認(rèn)識(shí)和定義Actions
Actions 相當(dāng)于組件中的methods,可以使用defineStore()中的actions屬性定義
expoer const useCounter = defineStore('counter',{ state: () => { counter:100, firstname:'kobe' }, getters:{ //調(diào)用其它Store doubleCounter(state){ return function (is) { return state.id + id } } }, actions:{ increment(){ this.counter++ }, //傳參 incrementnum(num){ this。counter += num } } })
和getters一樣,在action中可以通過this訪問整個(gè)store實(shí)例:
function increment(){ //調(diào)用 counterStore.increment() } function incrementnum(){ counterStore.increment(10) }
Actions執(zhí)行異步操作:
Actions中是支持異步操作的,并且我們可以編寫異步函數(shù),在函數(shù)中使用await
actions:{ async fetchHome(){ //???請(qǐng)求 const res = await fetch('?????') const data = await res.json() console.log('data',data) return data } }
cosnt counterStore = useCounter counterStore.fetchHome().then(res => { console.log(res) })
總結(jié)
到此這篇關(guān)于Vue3中Pinia基本使用的文章就介紹到這了,更多相關(guān)Vue3 Pinia使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Vue3-Ace-Editor如何在Vue3項(xiàng)目中集成代碼編輯器
這篇文章主要介紹了使用Vue3-Ace-Editor如何在Vue3項(xiàng)目中集成代碼編輯器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08vant-ui AddressEdit地址編輯和van-area的用法說明
這篇文章主要介紹了vant-ui AddressEdit地址編輯和van-area的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue中路由跳轉(zhuǎn)不計(jì)入history的操作
這篇文章主要介紹了vue中路由跳轉(zhuǎn)不計(jì)入history的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue自定義loader將行內(nèi)樣式px轉(zhuǎn)rem適配
這篇文章主要為大家介紹了vue自定義loader將行內(nèi)樣式px轉(zhuǎn)rem適配示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08vue項(xiàng)目如何設(shè)置全局字體樣式font-family
這篇文章主要介紹了vue項(xiàng)目如何設(shè)置全局字體樣式font-family問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11django簡(jiǎn)單的前后端分離的數(shù)據(jù)傳輸實(shí)例 axios
這篇文章主要介紹了django簡(jiǎn)單的前后端分離的數(shù)據(jù)傳輸實(shí)例 axios,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05vue+quasar使用遞歸實(shí)現(xiàn)動(dòng)態(tài)多級(jí)菜單
這篇文章主要為大家詳細(xì)介紹了vue+quasar使用遞歸實(shí)現(xiàn)動(dòng)態(tài)多級(jí)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07element中table操作按鈕展示與折疊的實(shí)現(xiàn)示例
因?yàn)殡S著功能的增多,table操作欄中的功能按鈕增多,這時(shí)候就需要折疊,本文主要介紹了element中table操作按鈕展示與折疊的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2022-04-04