Vue3使用Vuex之mapState與mapGetters詳解
1.如何使用?
在Vue中我們可以通過點語法很容易的獲取到Vuex中state的值,但當(dāng)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對象,將鼠標(biāo)移動至mapState()上查看

可以看到mapState接收的是一個字符串類型的數(shù)組,返回的是一個屬性為string類型,值為Computed類型的對象,可推導(dǎo)這里mapState接收的應(yīng)是["token", "username"]。const mappers = mapState(["token", "username"]);
再次將鼠標(biāo)移動至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'),點擊上圖藍(lán)色箭頭處查看報錯的源代碼

可以看出整個截圖部分是mapState對象,我們執(zhí)行mappers.token()是圖中標(biāo)紅的區(qū)域,再結(jié)合報錯信息可知報錯是因為這里的this對象沒有$store屬性。
由于this中只有$store被使用,并沒有用到this的其他屬性,則可以通過bind方式,手動傳一個具有store屬性的this對象進去,并輸出調(diào)用

這時我們可以看到token的值已經(jīng)在瀏覽器被輸出了,然后將bind()后的方法放到computed里執(zhí)行即可得到Ref類型的token對象。

以上就是Vue3使用Vuex之mapState與mapGetters詳解的詳細(xì)內(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使用.sync 實現(xiàn)父子組件的雙向綁定數(shù)據(jù)問題
這篇文章主要介紹了Vue使用.sync 實現(xiàn)父子組件的雙向綁定數(shù)據(jù),需要的朋友可以參考下2019-04-04
vue elementUI tree樹形控件獲取父節(jié)點ID的實例
今天小編就為大家分享一篇vue elementUI tree樹形控件獲取父節(jié)點ID的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue實踐---根據(jù)不同環(huán)境,自動轉(zhuǎn)換請求的url地址操作
這篇文章主要介紹了vue實踐---根據(jù)不同環(huán)境,自動轉(zhuǎn)換請求的url地址操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

