验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

Vue3怎么使用Vuex的mapState与mapGetters

阅读:1470 来源:乙速云 作者:代码code

Vue3怎么使用Vuex的mapState与mapGetters

1.如何使用?

Vue中我们可以通过点语法很容易的获取到Vuexstate的值,但当state数据比较多时,这样很不方便,可以借助mapState映射来简化;由于目前vuex的官网中提供的是Vue2的demo,下面说一下在Vue3中如何使用mapState
假设:在Vuex中的state具有tokenusername属性,现在要通过mapState取得tokenusername

Vue3+JS


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}

Vue3+TS(ps:建议直接使用pinia替代Vuex


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}

2.代码封装

storeMap.js

import { computed } from 'vue';
/**
* 映射store对应的属性(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 }

使用


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类型的对象

3.解题思路

以下是我的一种比较取巧的解题思路,可酌情参考

延用上面的例子
首先,我们先导入mapState,并创建一个空的mapState对象,将鼠标移动至mapState()上查看

Vue3怎么使用Vuex的mapState与mapGetters

可以看到mapState接收的是一个字符串类型的数组,返回的是一个属性为string类型,值为Computed类型的对象,可推导这里mapState接收的应是["token", "username"]
const mappers = mapState(["token", "username"]);

再次将鼠标移动至mappers上,查看mappers类型

Vue3怎么使用Vuex的mapState与mapGetters

可以看到mappers是一个对象;
console.log(mappers.token)查看属性token属性的值是什么

Vue3怎么使用Vuex的mapState与mapGetters

可以看到mappers.token是一个方法;
通过console.log(mappers.token())调用输出这个方法,发现浏览器的控制台报错了

Vue3怎么使用Vuex的mapState与mapGetters

错误提示Cannot read properties of undefined (reading 'state'),点击上图蓝色箭头处查看报错的源代码

Vue3怎么使用Vuex的mapState与mapGetters

可以看出整个截图部分是mapState对象,我们执行mappers.token()是图中标红的区域,再结合报错信息可知报错是因为这里的this对象没有$store属性。
由于this中只有$store被使用,并没有用到this的其他属性,则可以通过bind方式,手动传一个具有store属性的this对象进去,并输出调用

Vue3怎么使用Vuex的mapState与mapGetters


这时我们可以看到token的值已经在浏览器被输出了,然后将bind()后的方法放到computed里执行即可得到Ref类型的token对象。

Vue3怎么使用Vuex的mapState与mapGetters

分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>