vuex5中的Pinia插件機(jī)制
vuex5 Pinia插件機(jī)制
通過插件擴(kuò)展
- .給每個(gè)store添加公共屬性
- .給stores添加新的配置
- .給stores添加新的方法
- .包裹重用已有方法
- .改變或者取消actions
- .應(yīng)用額外的副作用像localstorage
- .應(yīng)用給指定的store
1、使用
import { createPinia } from 'pinia' const pinia = createPinia()
(1)定義插件
function SecretPiniaPlugin(context) { context.pinia; ?pina實(shí)例`createPinia()` context.app; ?vue實(shí)例`createApp()` context.store; ? 正在配置的store context.options; ?store的配置`defineStore()`
- (1)設(shè)置響應(yīng)式數(shù)據(jù)
每個(gè)store都是reactive包裹的對(duì)象,所以使用起來可直接解套ref
context.store.hello = ref('secret'); context.store.hello;
- (2)state添加數(shù)據(jù)
const globalSecret = ref('secret')
可直接添加
store.secret = globalSecret
通過$state,可獲得devtools追蹤、ssr中進(jìn)行序列化
store.$state.secret = globalSecret
添加第三方數(shù)據(jù),不要求響應(yīng)式時(shí),需要使用markRow進(jìn)行轉(zhuǎn)換
store.router = markRaw(router)
- (3)添加監(jiān)聽器
? store.$subscribe(() => { ? store改變時(shí)觸發(fā) ? }) ? store.$onAction(() => { ? ? ?action觸發(fā)時(shí)觸發(fā) ? }) ... }
(2)應(yīng)用插件
pinia.use(SecretPiniaPlugin)
(3)devTools能追蹤修改
方式一:返回修改的操作
pinia.use(({ store }) => ({ ? store.hello = 'world' }))
方式二:顯示添加
pinia.use(({ store }) => { ? store.hello = 'world' ? if (process.env.NODE_ENV === 'development') { ? ? store._customProperties.add('hello') ? } })
2、應(yīng)用
(1)給每個(gè)store添加公共state
function SecretPiniaPlugin() { ? return { secret: 'the cake is a lie' } } pinia.use(SecretPiniaPlugin)
(2)改寫store中的action
.此例為改寫成防抖action
defineStore('search', { ? actions: { ? ? searchContacts() { ? ? }, ? }, ? debounce: { ? ? searchContacts: 300, ? }, })
對(duì)于函數(shù)寫法的store,自定義選項(xiàng)放入第三個(gè)參數(shù)中
defineStore( ? 'search', ? () => { ? ? ... ? }, ? { ? ? // this will be read by a plugin later on ? ? debounce: { ? ? ? // debounce the action searchContacts by 300ms ? ? ? searchContacts: 300, ? ? }, ? } )
插件中:
import debounce from 'lodash/debunce' pinia.use(({ options, store }) => { ? if (options.debounce) { ?? ? ? 將設(shè)置了debounce的store中的原action改寫成具有防抖功能的action ? ?? ? ? return Object.keys(options.debounce).reduce((debouncedActions, action) => { ? ? ? debouncedActions[action] = debounce( ? ? ? ? store[action], ? ? ? ? options.debounce[action] ? ? ? ) ? ? ? return debouncedActions ? ? }, {}) ? } })
pinia和vuex的區(qū)別
(1)它沒有mutation,他只有state,getters,action【同步、異步】使用他來修改state數(shù)據(jù)
(2)他默認(rèn)也是存入內(nèi)存中,如果需要使用本地存儲(chǔ),在配置上比vuex麻煩一點(diǎn)
(3)語法上比vuex更容易理解和使用,靈活。
(4)pinia沒有modules配置,沒一個(gè)獨(dú)立的倉庫都是definStore生成出來的
(5)state是一個(gè)對(duì)象返回一個(gè)對(duì)象和組件的data是一樣的語法
需要在頁面組件中引入我們要修改數(shù)據(jù)
安裝的本地存儲(chǔ)插件可以是npm也可以是year
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue移動(dòng)端實(shí)現(xiàn)紅包雨效果
這篇文章主要為大家詳細(xì)介紹了vue移動(dòng)端實(shí)現(xiàn)紅包雨效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07使用vue-i18n?入口文件配置控制臺(tái)報(bào)警問題解決
這篇文章主要介紹了使用vue-i18n?入口文件配置控制臺(tái)報(bào)警問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue如何使用async、await實(shí)現(xiàn)同步請(qǐng)求
這篇文章主要介紹了vue如何使用async、await實(shí)現(xiàn)同步請(qǐng)求,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12vue中post請(qǐng)求報(bào)400的解決方案
這篇文章主要介紹了vue中post請(qǐng)求報(bào)400的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10動(dòng)態(tài)加載權(quán)限管理模塊中的Vue組件
本篇文章給大家詳細(xì)講解了如何在權(quán)限管理模塊中動(dòng)態(tài)的加載VUE組件的過程,有這方面需求的朋友跟著學(xué)習(xí)下吧。2018-01-01Vue2?this?能夠直接獲取到?data?和?methods?的原理分析
這篇文章主要介紹了Vue2?this能夠直接獲取到data和methods的原理分析,因?yàn)閙ethods里的方法通過bind指定了this為new?Vue的實(shí)例2022-06-06vue-router結(jié)合vuex實(shí)現(xiàn)用戶權(quán)限控制功能
這篇文章主要介紹了vue-router結(jié)合vuex實(shí)現(xiàn)用戶權(quán)限控制功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11