如何使用Vue mapState快捷獲取Vuex state多個值
如何在 Vue3 中更方便快捷地 獲取 Vuex 中state 中的多個值
假設 在 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中計算
// vue2 computed:{ ...mapState(["name","age","height","sex"]), }
Vue3 中提供了 useStore() 進行獲取
但不能直接像Vue2 那樣通過mapState() 進行一次性獲取多個值,只能一次獲取一個
// 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 多個值
需要解決的問題:
- 1.setup中沒有this 指向
- 2.mapState的返回值
解決辦法
mapState 返回值 是一個對象,{name:function,age:function},對象的值是一個函數(shù),恰好computed 的參數(shù)可以是函數(shù)返回值,只要解決了 computed中的$store 指向就可以,恰好 Vue3提供了 useStore(),然后使用apply() 或者 bind() 進行 指向
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中計算state時需要 $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(); // 獲取相應的對象 : {name:function,age:function} const storeStateFns = mapState(mapper); // 進行 $store 指向 ,并賦值 const storeState = {} Object.keys(storeStateFns).forEach(Fnkey => { // setup中無this 指向,在 compute中計算state時需要 $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ù)名重復 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多個值的詳細內(nèi)容,更多關(guān)于Vue mapState使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue中動態(tài)添加style樣式的幾種寫法總結(jié)
這篇文章主要介紹了vue中動態(tài)添加style樣式的幾種寫法總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue-cli3.0如何使用CDN區(qū)分開發(fā)、生產(chǎn)、預發(fā)布環(huán)境
這篇文章主要介紹了vue-cli3.0如何使用CDN區(qū)分開發(fā)、生產(chǎn)、預發(fā)布環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11vue代理請求之Request?failed?with?status?code?404問題及解決
這篇文章主要介紹了vue代理請求之Request?failed?with?status?code?404問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Vue中的無限加載vue-infinite-loading的方法
本篇文章主要介紹了Vue中的無限加載vue-infinite-loading的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04VueJS 集成 Medium Editor的示例代碼 (自定義編輯器按鈕)
本篇文章主要介紹了VueJS 集成 Medium Editor的示例代碼 (自定義編輯器按鈕),具有一定的參考價值,有興趣的可以了解一下2017-08-08利用vue + element實現(xiàn)表格分頁和前端搜索的方法
眾所周知Element 是一套 Vue.js 后臺組件庫,它能夠幫助你更輕松更快速地開發(fā)后臺項目。下面這篇文章主要給大家介紹了關(guān)于利用vue + element實現(xiàn)表格分頁和前端搜索的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12