" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何使用Vue mapState快捷獲取Vuex state多個值

 更新時間:2023年06月13日 10:27:14   作者:阿淮iya  
這篇文章主要為大家介紹了如何使用Vue mapState快捷獲取Vuex state多個值實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

如何在 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)文章

最新評論