Vue3使用Vuex之mapState與mapGetters詳解
1.如何使用?
在Vue
中我們可以通過點語法很容易的獲取到Vuex
中state
的值,但當state
數(shù)據(jù)比較多時,這樣很不方便,可以借助mapState映射來簡化;由于目前vuex
的官網(wǎng)中提供的是Vue2
的demo,下面說一下在Vue3中如何使用mapState
。
假設(shè):在Vuex
中的state
具有token
和username
屬性,現(xiàn)在要通過mapState
取得token
和username
:
Vue3+JS
<script setup> import { useStore, mapState } from "vuex"; import { computed } from "vue"; const store = useStore(); const mappers = mapState(["token", "username"]); const mapData = {} Object.keys(mappers).forEach((item) => { mapData[item] = computed(mappers[item].bind({ $store:store })); }); console.log(mapData) // ref類型的mapData對象:{token: ComputedRefImpl, username:ComputedRefImpl} </script>
Vue3+TS(ps:建議直接使用pinia
替代Vuex
)
<script setup lang="ts"> import { useStore, mapState } from "vuex"; import { computed } from "vue"; import type { Ref } from 'vue' type mappersType = { token:() => any, username:() => any, [propName:string]:any } type mapDataType = { token:Ref, username:Ref, [propName:string]:Ref } const store = useStore(); const mappers:mappersType = mapState(["token", "username"]) as mappersType const mapData:mapDataType = {} as mapDataType; Object.keys(mappers).forEach((item) => { mapData[item] = computed(mappers[item].bind({ $store:store })); }); console.log(mapData) // ref類型的mapData對象:{token: ComputedRefImpl, username:ComputedRefImpl} </script>
2.代碼封裝
storeMap.js
import { computed } from 'vue'; /** * 映射store對應(yīng)的屬性(mapState|mapGetters) * @param $store useStore() * @param mappers mapState([])|mapGetters([]) * @returns {[propName:string]: ComputedRefImpl,...} */ function getStoreMap($store, mappers) { const mapData = {} Object.keys(mappers).forEach((item) => { mapData[item] = computed(mappers[item].bind({ $store })).value; }); return mapData; } export { getStoreMap }
使用
<script setup> import { useStore, mapState, mapGetters } from "vuex"; import { getStoreMap } from './storeMap' const store = useStore(); const mappers = mapState(["token", "username"]); // const mappers = mapGetters(["getToken", "getUsername"]); //也可以獲取mapGetters const mapData = getStoreMap(store, mappers) console.log(mapData) // 包含state或getters屬性的ref類型的對象 </script>
3.解題思路
以下是我的一種比較取巧的解題思路,可酌情參考
延用上面的例子
首先,我們先導(dǎo)入mapState
,并創(chuàng)建一個空的mapState
對象,將鼠標移動至mapState()
上查看
可以看到mapState
接收的是一個字符串類型的數(shù)組,返回的是一個屬性為string
類型,值為Computed
類型的對象,可推導(dǎo)這里mapState
接收的應(yīng)是["token", "username"]
。const mappers = mapState(["token", "username"])
;
再次將鼠標移動至mappers上,查看mappers類型
可以看到mappers是一個對象;console.log(mappers.token)
查看屬性token屬性的值是什么
可以看到mappers.token
是一個方法;
通過console.log(mappers.token())
調(diào)用輸出這個方法,發(fā)現(xiàn)瀏覽器的控制臺報錯了
錯誤提示Cannot read properties of undefined (reading 'state')
,點擊上圖藍色箭頭處查看報錯的源代碼
可以看出整個截圖部分是mapState對象,我們執(zhí)行mappers.token()
是圖中標紅的區(qū)域,再結(jié)合報錯信息可知報錯是因為這里的this
對象沒有$store
屬性。
由于this中只有$store
被使用,并沒有用到this
的其他屬性,則可以通過bind
方式,手動傳一個具有store
屬性的this
對象進去,并輸出調(diào)用
這時我們可以看到token
的值已經(jīng)在瀏覽器被輸出了,然后將bind()
后的方法放到computed
里執(zhí)行即可得到Ref
類型的token
對象。
以上就是Vue3使用Vuex之mapState與mapGetters詳解的詳細內(nèi)容,更多關(guān)于Vue3 Vuex mapState mapGetters的資料請關(guān)注腳本之家其它相關(guān)文章!
- 記一次vuex的mapGetters無效原因及解決
- vuex中...mapstate和...mapgetters的區(qū)別及說明
- vue3.0使用mapState,mapGetters和mapActions的方式
- vuex 中輔助函數(shù)mapGetters的基本用法詳解
- vuex2中使用mapGetters/mapActions報錯的解決方法
- vuex中的 mapState,mapGetters,mapActions,mapMutations 的使用
- 詳解vuex中mapState,mapGetters,mapMutations,mapActions的作用
- Vue Getters和mapGetters的原理及使用示例詳解
相關(guān)文章
Vue中watch監(jiān)聽第一次不觸發(fā)、深度監(jiān)聽問題
這篇文章主要介紹了Vue中watch監(jiān)聽第一次不觸發(fā)、深度監(jiān)聽問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10利用vue和element-ui設(shè)置表格內(nèi)容分頁的實例
下面小編就為大家分享一篇利用vue和element-ui設(shè)置表格內(nèi)容分頁的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03