如何使用Vue mapState快捷獲取Vuex state多個(gè)值
如何在 Vue3 中更方便快捷地 獲取 Vuex 中state 中的多個(gè)值
假設(shè) 在 state 存在值,需要全部獲取
state(){
return {
name:'huai',
age:'18',
height:'180',
sex:'男'
}
}
頁面中展示
<p>{{name}}</p>
<p>{{age}}</p>
<p>{{height}}</p>
<p>{{sex}}</p>Vue2 可以直接使用 mapState 直接放到 computed中計(jì)算
// vue2
computed:{
...mapState(["name","age","height","sex"]),
}
Vue3 中提供了 useStore() 進(jìn)行獲取
但不能直接像Vue2 那樣通過mapState() 進(jìn)行一次性獲取多個(gè)值,只能一次獲取一個(gè)
// vue3
import {computed} from 'vue'
import { useStore} from 'vuex';
setup(){
const store = useStore();
const name = computed(()=>store.state.name);
const age = computed(()=>store.state.age);
const height = computed(()=>store.state.height);
const sex = computed(()=>store.state.sex);
return {
name,
age,
height,
sex
}
}如何在 Vue3 中 使用 mapState() 一次獲取vuex中 state 多個(gè)值
需要解決的問題:
- 1.setup中沒有this 指向
- 2.mapState的返回值
解決辦法
mapState 返回值 是一個(gè)對象,{name:function,age:function},對象的值是一個(gè)函數(shù),恰好computed 的參數(shù)可以是函數(shù)返回值,只要解決了 computed中的$store 指向就可以,恰好 Vue3提供了 useStore(),然后使用apply() 或者 bind() 進(jìn)行 指向
import { computed } from 'vue';
import { useStore , mapState } from 'vuex';
setup(){
const store = useStore();
// 傳入數(shù)組
const storeStateFns = mapState(["name","age","height","sex"]);
const storeState ={};
Object.keys(storeStateFns).forEach(Fnkey => {
// setup中無this 指向,在 compute中計(jì)算state時(shí)需要 $store 指向 ,所以使用bind() 綁定 $store
const fn = storeStateFns[Fnkey].bind({ $store: store });
storeState[Fnkey] = computed(fn)
})
return{
// 解構(gòu)
...storeState
}
}封裝
// useState.js
import { useStore, mapState } from 'vuex'
import { computed } from 'vue';
export default function(mapper) {
// 獲取key,并判斷是否存在
const store = useStore();
// 獲取相應(yīng)的對象 : {name:function,age:function}
const storeStateFns = mapState(mapper);
// 進(jìn)行 $store 指向 ,并賦值
const storeState = {}
Object.keys(storeStateFns).forEach(Fnkey => {
// setup中無this 指向,在 compute中計(jì)算state時(shí)需要 $store 指向 ,所以使用bind() 綁定 $store
const fn = storeStateFns[Fnkey].bind({ $store: store });
storeState[Fnkey] = computed(fn)
})
// 返回值
return storeState;
}使用
import hookStoreState from './useState.js'
export default {
setup() {
// 傳入數(shù)組
const storeStateArr = hookStoreState(["name", "age", "counter"]);
// 傳入對象,可以避免參數(shù)名重復(fù)
const storeStateObj = hookStoreState({
sName: (state) => state.name,
sAge: (state) => state.age,
sHeight: (state) => state.height,
sSex:(state)=>state.sex
});
return {
...storeStateArr,
...storeStateObj,
};
},
};以上就是如何使用Vue mapState快捷獲取Vuex state多個(gè)值的詳細(xì)內(nèi)容,更多關(guān)于Vue mapState使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于配置babel-plugin-import報(bào)錯(cuò)的坑及解決
這篇文章主要介紹了關(guān)于配置babel-plugin-import報(bào)錯(cuò)的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
axios無法加載響應(yīng)數(shù)據(jù):no?data?found?for?resource?with?given?i
最近在在做一個(gè)小查詢功能的時(shí)候踩了一個(gè)坑,所以這篇文章主要給大家介紹了關(guān)于axios無法加載響應(yīng)數(shù)據(jù):no?data?found?for?resource?with?given?identifier報(bào)錯(cuò)的解決方法,需要的朋友可以參考下2022-11-11
vue深拷貝的3種實(shí)現(xiàn)方式小結(jié)
當(dāng)使用同一個(gè)對象產(chǎn)生沖突時(shí),可以使用lodash包,對該對象進(jìn)行深拷貝,從而使操作的對象為不同的對象,這篇文章主要給大家介紹了關(guān)于vue深拷貝的3種實(shí)現(xiàn)方式,需要的朋友可以參考下2023-02-02

