Vue3 使用mapState

简介: 【10月更文挑战第8天】

如何在 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"]),
}

2.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 多个值呢,
3.1. 需要解决的问题: 1.setup中没有this 指向 2.mapState的返回值
3.2. 解决办法
mapState 返回值 是一个对象,{name:function,age:function},对象的值是一个函数,恰好computed 的参数可以是函数返回值,只要解决了 computed中的$store 指向就可以,恰好 Vue3提供了 useStore(),然后使用apply() 或者 bind() 进行 指向

import {
    computed } from 'vue';
import {
    useStore , mapState } from 'vuex';

    setup(){
   
        const store = useStore();
        // 传入数组
        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{
   
             // 解构
            ...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() {
   
    // 传入数组
    const storeStateArr = hookStoreState(["name", "age", "counter"]);
    // 传入对象,可以避免参数名重复
    const storeStateObj = hookStoreState({
   
      sName: (state) => state.name,
      sAge: (state) => state.age,
      sHeight: (state) => state.height,
      sSex:(state)=>state.sex
    });
    return {
   
      ...storeStateArr,
      ...storeStateObj,
    };
  },
};
相关文章
|
2天前
vue3+Ts 二次封装ElementUI form表单
【10月更文挑战第8天】
105 56
|
3天前
|
Web App开发 JavaScript 数据可视化
vue3扩展echart封装为组件库-快速复用
vue3扩展echart封装为组件库-快速复用
14 7
|
4天前
|
前端开发 JavaScript API
Vue3商品SKU多规格编辑组件
Vue3商品SKU多规格编辑组件
22 5
|
2天前
|
JavaScript iOS开发
用上Vue3,你真的变了吗?
用上Vue3,你真的变了吗?
9 1
|
2天前
|
缓存 JavaScript 前端开发
对比一下Vue2和Vue3?
本文首发于微信公众号“前端徐徐”,详细对比了 Vue 2 和 Vue 3 在原理、生命周期、性能、编码方式、API、Diff 算法、打包构建、TS 支持等八个方面的差异,帮助读者全面了解两者的不同之处。
10 0
对比一下Vue2和Vue3?
|
2天前
|
JavaScript API 开发者
十分钟 带你强势入门 vue3
十分钟 带你强势入门 vue3
11 1
|
4天前
|
存储 消息中间件 JavaScript
Vue3 多种组件通讯方法
【10月更文挑战第6天】
vue3日历组件库Vue-Cel使用
vue3日历组件库Vue-Cel使用
|
4天前
|
JavaScript 索引
Vue 3 数组变更详解:哪些操作会修改原数组?| 笔记
在处理数组时,了解哪些操作会修改原数组,哪些操作不会修改原数组,对高效编写 Vue 应用程序至关重要。本文将详细介绍 Vue 3 中的常见数组操作,并按照是否会修改原数组进行分类说明。
24 2
|
2天前
|
存储 JavaScript 前端开发
Vue中组件通信方式有哪些?
本文介绍了 Vue 中几种常见的组件间通信方式,包括 Props / $emit、provide / inject、ref / refs、eventBus、Vuex、$parent / $children、$attrs / $listeners 以及通过 vue-router 传参。每种方式都有其适用场景和注意事项,帮助开发者根据具体需求选择合适的通信方式。
10 3
Vue中组件通信方式有哪些?