vue3中使用pinia(大菠蘿)狀態(tài)管理倉庫的項目實踐
在Vue 3中,狀態(tài)管理是非常重要的一部分。而Pinia(大菠蘿)作為一個全新的狀態(tài)管理庫,在Vue 3中提供了更好的狀態(tài)管理方案,可以方便地實現(xiàn)任意組件之間數(shù)據(jù)共享。與VueX不同的是,Pinia并不依賴于Vue 3的響應式系統(tǒng),而是通過手動訂閱和派發(fā)事件的方式來實現(xiàn)狀態(tài)管理。這樣可以提高系統(tǒng)性能,減少數(shù)據(jù)冗余,同時使得代碼更容易測試和維護。本文將介紹如何在Vue 3中使用Pinia狀態(tài)管理倉庫,并提供一些最佳實踐來幫助您更好地使用它。
一、vue3中使用pinia
Pinia是一個Vue 3的狀態(tài)管理庫,提供了一個易于使用的API和可擴展性。下面是如何在Vue 3項目中使用Pinia的步驟:
安裝Pinia
在項目中使用npm或yarn安裝Pinia:
npm install pinia
或
yarn add pinia
創(chuàng)建和注冊一個Pinia store
創(chuàng)建一個store并導出它,例如:
import { defineStore } from 'pinia' export const useMyStore = defineStore('myStore', { ? state: () => ({ ? ? count: 0, ? }), ? actions: { ? ? increment() { ? ? ? this.count++ ? ? }, ? }, })
在項目中的任何組件中使用該store,只需導入并使用它:
import { useMyStore } from './store' export default { ? setup() { ? ? const myStore = useMyStore() ? ? return { ? ? ? count: myStore.count, ? ? ? increment: myStore.increment, ? ? } ? }, }
在Vue應用程序中安裝Pinia
在Vue的createApp時,將Pinia安裝為插件:
import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' const app = createApp(App) app.use(createPinia()) app.mount('#app')
現(xiàn)在,您可以在Vue應用程序中使用Pinia啦!
備注:由于Vue 3的Composition API的特性,您可以使用useXXX函數(shù)(例如 useMyStore())而不是傳統(tǒng)的mapState、mapActions等選項來訪問store中的狀態(tài)和操作。
二、使用pinia實現(xiàn)任意組件之間數(shù)據(jù)共享
要在Vue 3項目中使用Pinia實現(xiàn)任意組件之間的數(shù)據(jù)共享,您可以按照以下步驟操作:
創(chuàng)建一個名為user的Pinia store,例如:
import { defineStore } from 'pinia' export const useUserStore = defineStore('user', { ? state: () => ({ ? ? name: 'John Doe', ? }), ? actions: { ? ? setName(name) { ? ? ? this.name = name ? ? }, ? }, })
在Vue應用程序中注冊Pinia store,您需要在入口文件中創(chuàng)建Pinia實例并將其注冊到Vue應用程序中。例如:
import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' import { useUserStore } from './store/user' const app = createApp(App) app.use(createPinia()) app.provide('userStore', useUserStore()) app.mount('#app')
在上面的示例中,我們使用provide API將useUserStore()實例注冊為名為’userStore’的全局可注入實例。
在組件中使用Pinia store,您可以在任何Vue組件中使用創(chuàng)建的Pinia store。例如,在組件A和組件B中,您可以使用以下方式導入user store:
import { useUserStore } from '../store/user' export default { ? setup() { ? ? const userStore = useUserStore() ? ? return { ? ? ? name: userStore.name, ? ? ? setName: userStore.setName, ? ? } ? }, }
在上面的示例中,我們使用useUserStore()函數(shù)從’./store/user.js’中導入我們的Pinia store,并在setup()函數(shù)中使用store的屬性和操作。
在組件A或組件B中更新user store中的狀態(tài),例如:
import { useUserStore } from '../store/user' export default { ? setup() { ? ? const userStore = useUserStore() ? ? const handleNameChange = (name) => { ? ? ? userStore.setName(name) ? ? } ? ? return { ? ? ? name: userStore.name, ? ? ? handleNameChange, ? ? } ? }, }
在上面的示例中,我們導入’…/store/user.js’中的user store,并在組件A中使用setName()操作更改store中的狀態(tài)。
在組件B中讀取user store中的狀態(tài),例如:
import { useUserStore } from '../store/user' export default { ? setup() { ? ? const userStore = useUserStore() ? ? return { ? ? ? name: userStore.name, ? ? } ? }, }
在上面的示例中,我們導入’…/store/user.js’中的user store,并在組件B中使用store的name屬性。
現(xiàn)在,您已經(jīng)成功地在Vue 3項目中使用Pinia實現(xiàn)了任意組件之間的數(shù)據(jù)共享!
三、pinia中的getters
在Vue 3項目中,使用Pinia狀態(tài)管理倉庫,可以使用getter來獲取存儲狀態(tài),這是一個非常有用的特性。getter可以用于計算或轉(zhuǎn)換存儲的狀態(tài),并且能夠更新UI或在其他地方使用。
以下是一個使用getter的示例:
import { defineStore } from 'pinia' export const useUserStore = defineStore('user', { ? state: () => ({ ? ? firstName: 'John', ? ? lastName: 'Doe', ? }), ? getters: { ? ? fullName() { ? ? ? return `${this.firstName} ${this.lastName}` ? ? }, ? }, })
在上面的例子中,我們在user store中定義了一個getter函數(shù)fullName。getter函數(shù)返回由firstName和lastName組合成的完整名稱。
現(xiàn)在,可以在組件中使用getter來獲取完整名稱,并將其顯示在UI中,如下所示:
<template> ? <div>{{ fullName }}</div> </template> <script> import { useUserStore } from '../store/user' export default { ? setup() { ? ? const userStore = useUserStore() ? ? return { ? ? ? fullName: userStore.fullName, ? ? } ? }, } </script>
在上述代碼中,我們在組件中使用useUserStore鉤子獲取store實例,然后返回完整名稱getter。最后,在模板中將fullName綁定到UI元素,以便在屏幕上顯示完整名稱。
通過使用getter,您可以輕松地計算和轉(zhuǎn)換狀態(tài),并將其暴露給Vue組件中使用。在實際開發(fā)中,getter的應用場景非常廣泛,比如處理復雜的計算邏輯,根據(jù)狀態(tài)屬性的值決定下一步要執(zhí)行的操作等等。
四、pinia的組合式寫法
在Vue 3項目中,除了可以使用Options API來定義Pinia狀態(tài)管理倉庫之外,還可以使用Composition API。下面是一個使用Composition API來定義和使用Pinia狀態(tài)管理倉庫的示例:
//定義組合式API倉庫 import { defineStore } from "pinia"; import { ref, computed,watch} from 'vue'; //創(chuàng)建小倉庫 let useTodoStore = defineStore('todo', () => { ? ? let todos = ref([{ id: 1, title: '吃飯' }, { id: 2, title: '睡覺' }, { id: 3, title: '打豆豆' }]); ? ? let arr = ref([1,2,3,4,5]); ? ? const total = computed(() => { ? ? ? ? return arr.value.reduce((prev, next) => { ? ? ? ? ? ? return prev + next; ? ? ? ? }, 0) ? ? }) ? ? //務必要返回一個對象:屬性與方法可以提供給組件使用 ? ? return { ? ? ? ? todos, ? ? ? ? arr, ? ? ? ? total, ? ? ? ? updateTodo() { ? ? ? ? ? ? todos.value.push({ id: 4, title: '組合式API方法' }); ? ? ? ? } ? ? } }); export default useTodoStore;
上述代碼中使用ref定義的todos 和 arr 就相當于選項式中的state中的數(shù)據(jù);使用computed計算屬性計算的total就相當于選項式中的getters中的屬性;updateTodo方法就相當于寫在選項式actions中的方法
五、pinia和vuex的對比
需要注意的是,Vue3 中的 Pinia 不僅支持 Vue2 中 Vuex 所有的功能,而且使用起來更加方便,對 TypeScript 的支持也更加友好。而且 Pinia 只依賴于 Vue3 的響應式系統(tǒng),所以可以很容易地跨平臺使用。
特性 | Vuex (Vue2) | Pinia (Vue3) |
---|---|---|
數(shù)據(jù)存儲 | State 存儲在 Store 中 | State 存儲在 Store 中 |
數(shù)據(jù)修改 | commit/mutation | action |
響應式 | 使用 Vue 響應式系統(tǒng)實現(xiàn) | 使用 Vue 3 的響應式系統(tǒng)實現(xiàn) |
數(shù)據(jù)獲取 | getters | getters |
模塊化 | 模塊按照功能劃分,每個模塊有自己的 state、mutation、action 和 getter | 模塊按照功能劃分,每個模塊有自己的 state、action 和 getter |
TypeScript 支持 | 需要額外安裝 @vue/cli-plugin-typescript 插件,并在 store 中進行類型定義 | 內(nèi)置 TypeScript 支持,使用起來更加方便 |
插件 | 支持插件(例如 logger 插件) | 支持插件(例如 devtools 插件) |
熱重載 | 支持熱重載 | 支持熱重載 |
六、總結(jié)
綜上所述,Pinia(大菠蘿)是Vue 3中一個非常重要的狀態(tài)管理庫,它能夠幫助我們更好地管理和共享組件中的數(shù)據(jù)。通過本文的介紹,您已經(jīng)學會了如何在Vue 3中使用Pinia,以及一些最佳實踐來幫助您更好地使用它。當然,這只是入門級別的介紹,并且Pinia仍然有很多特性和功能可以探索。如果您想深入學習更多關(guān)于Pinia的內(nèi)容,建議您查閱官方文檔。
到此這篇關(guān)于vue3中使用pinia(大菠蘿)狀態(tài)管理倉庫的文章就介紹到這了,更多相關(guān)vue3 pinia狀態(tài)管理倉庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue?watch中監(jiān)聽值的變化,判斷后修改值方式
這篇文章主要介紹了Vue?watch中監(jiān)聽值的變化,判斷后修改值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue-cli 使用axios的操作方法及整合axios的多種方法
這篇文章主要介紹了vue-cli 使用axios的操作方法及整合axios的多種方法,vue-cli整合axios的多種方法,小編一一給大家列出來了,大家根據(jù)自身需要選擇,需要的朋友可以參考下2018-09-09