vue3.0下如何使用mapState,mapGetters和mapActions
vue3.0下如何使用mapState,mapGetters和mapActions
- 1、新建useMapper.js
- 2、新建useState.js
- 3、新建useGetters.js
- 4、新建useActions.js
- 5、页面中使用
vue2.0中使用mapState及mapActions的方式
// 使用mapState
computed: { ...mapState({ //... })
}methods: {...mapActions(['fnA', 'fnB'])
}
vue3.0中获取state和使用actions的方式
import {computed} from 'vue'
import {useStore} from 'vuex'setup() {const store = useStore();const stateA = computed(() => store.state.stateA);const stateB = computed(() => store.state.stateB);const methodA = store.dispatch('methodA', {name: '张三'});
}
那如何才能在vue3下使用mapState这些api呢?
答案是封装mapState,mapGetters,mapActions方法。
1、新建useMapper.js
import { useStore } from 'vuex'
import { computed } from 'vue'export function useStateMapper(mapper, mapFn) {const store = useStore();const storeStateFns = mapFn(mapper);const storeState = {};Object.keys(storeStateFns).forEach(fnKey => {// vuex源码中mapState和mapGetters的方法中使用的是this.$store,所以更改this绑定const fn = storeStateFns[fnKey].bind({ $store: store });storeState[fnKey] = computed(fn)})return storeState
}export function useActionMapper(mapper, mapFn) {const store = useStore();const storeActionsFns = mapFn(mapper);const storeAction = {};Object.keys(storeActionsFns).forEach(fnKey => {storeAction[fnKey] = storeActionsFns[fnKey].bind({ $store: store })})return storeAction
}
2、新建useState.js
import { mapState, createNamespacedHelpers } from 'vuex'import { useStateMapper } from './useMapper'
import {checkType} from './index'
/*** * @param {*} moduleName 模块名称* @param {*} mapper state属性集合 ['name', 'age']* @returns */
export function useState(moduleName, mapper) {let mapperFn = mapState;// 如果使用模块化,则使用vuex提供的createNamespacedHelpers方法找到对应模块的mapState方法if (checkType(moduleName) === "[object String]" && moduleName.length > 0) {mapperFn = createNamespacedHelpers(moduleName).mapState}return useStateMapper(mapper, mapperFn)
}
3、新建useGetters.js
import { mapGetters, createNamespacedHelpers } from 'vuex'import { useStateMapper } from './useMapper'
import {checkType} from './index'
/*** * @param {*} moduleName 模块名称* @param {*} mapper getters属性集合 ['name', 'age']* @returns */
export function useGetters(moduleName, mapper) {let mapperFn = mapGetters;// 如果使用模块化,则使用vuex提供的createNamespacedHelpers方法找到对应模块的mapGetters方法if (checkType(moduleName) === "[object String]" && moduleName.length > 0) {mapperFn = createNamespacedHelpers(moduleName).mapGetters}return useStateMapper(mapper, mapperFn)
}
4、新建useActions.js
import { mapActions, createNamespacedHelpers } from 'vuex';
import {useActionMapper} from './useMapper'
import {checkType} from './index'
/*** * @param {*} moduleName 模块名称* @param {*} mapper 方法名集合 ['fn1', 'fn2']* @returns */
export function useActions(moduleName, mapper) {let mapperFn = mapActions;// 如果使用模块化,则使用vuex提供的createNamespacedHelpers方法找到对应模块的mapActions方法if (checkType(moduleName) === "[object String]" && moduleName.length > 0) {mapperFn = createNamespacedHelpers(moduleName).mapActions}return useActionMapper(mapper, mapperFn)
}
5、页面中使用
<template><div class="home"><span>姓名:{{name}} 年龄:{{age}} 性别:{{sex}}</span><button @click="changeName">改名</button></div>
</template><script>
// @ is an alias to /src
import {useState} from '@/utils/useState'
import {useActions} from '@/utils/useAction'
export default {name: "home",setup() {const storeState = useState('home', ['name', 'age', 'sex'])const storeActions = useActions('home', ['setName'])const changeName = () => {storeAction.setName('李四')}return {changeName,...storeState,...storeActions};},
};
</script>
vue3.0下如何使用mapState,mapGetters和mapActions相关推荐
- vue——vuex mapState,mapGetters,mapMutations,mapActions
当多次获取state中的数据时会出现大量的 $store.state.属性名 造成代码的冗余同时也很麻烦.就可使用 mapState(引用时使用的名称,'state中的属性名') 或 mapState ...
- Vue教程5【vuex】getters,mapState,mapGetters,mapActions,mapMutations,模块化namespace
vuex 什么时候需要使用? 状态(数据)共享! vuex工作原理 搭建vuex环境 创建文件 src/store/index.js//全局安装[--save生产环境] npm install -g ...
- [vue] Vuex中四个map方法的使用 mapState mapGetters mapActions mapMutations
1. mapState方法: 用于帮助我们映射state中的数据为计算属性 computed: {//借助mapState生成计算属性:sum.school.subject(对象写法)...mapSt ...
- 【笔记整理】vuex介绍和原理以及mapState与mapGetters、mapActions与mapMutations
vuex 理解Vuex 概念:专门在 Vue 中实现集中式状态(数据)管理的一个 Vue 插件,对 vue 应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式,且适用于任意组 ...
- vuex的mapState,mapGetters,mapActions,mapMutations与模块化
mapState 和mapGetters 用于生成计算属性computed mapState({"计算属性名":"State数据","xxx" ...
- mapState mapGetters
1. mapState方法:用于帮助我们映射state中的数据为计算属性 computed: //借助mapState生成计算属性: sum. school. subject (对象写法)... ma ...
- Vue3 直接使用Vuex的mapState和mapGetters时报错的分析及解决方案
Vuex 提供了 mapState.mapGetters.mapActions 和 mapMutations 四个函数,其返回结果分别是 mappedState,mappedGetter,mapped ...
- Vue3.0+Cesium+Tomcat服务下倾斜摄影数据加载详细过程
Vue3.0+Cesium+Tomcat服务下倾斜摄影数据加载 1.Vue-cli 3.0 + cesium 构建 参考资料地址Vue-cli 3.0 + cesium 构建 注意,因为文档中设置默认 ...
- Vue3.0 使用 ref 判断目标 node 区域之外的点击事件(实现下拉框、弹窗关闭功能)
有时候会遇到点击目标区域绑定事件 / 展示效果,然后点击目标区域之外的地方就关闭效果 / 触发另一事件,为了实现这样的功能,我们需要通过比对 node 节点来进行判断,在下面我会使用 Vue3.0 的 ...
最新文章
- 传统软件的云计算之路
- jQuery Object 和 HTML Element间的转换
- Docker之Dockerfile详解
- 前端学习(1927)vue之电商管理系统电商系统之美化一层循环的UI结构for循环渲染第三层结构
- 《是碰巧还是执着?python所阅读的每一场知识点,唯一的共同点就是——参赛选手中,有详解Python的装饰器!》
- Bootstrap列表组的情景类
- html找不到定义,Main无法正常使用,找不到它的定义
- C语言解决累加和累乘问题
- Private Bytes,Working Set,Virtual Size的区别
- 关于学校软件安装错误:“an error ocurred installing TAP device”的个人解决办法
- H5 调用企业微信API
- python课程计算bmi_Python第十一课 计算体脂率1.0
- ATM自动取款机系统的功能需求分析
- vue之解决跨域问题
- java服务cpu突然飙升排查
- □ 影片名:《陈好-女人要爱自己》(7012) 在线播放
- 解决win10 自动同步时间灰色
- 关于Qt 5-MSVC 2015 64位在 win7 64位系统debug程序崩溃的问题
- android原生打印PDF,HTML;HTML转换为PDF
- 比 Elasticsearch 更快!RediSearch + RedisJSON = 王炸
热门文章
- 旺铺html框架代码,阿里旺铺装修代码的fx.accordion组件使用详解及HTML模板
- Simens NX (原UG)内部代码逆向分析 / Inner code Reverse analysis of NX software
- 电视台计算机网络,计算机网络在电视台中的应用论文
- 微信小程序填坑之路(三)--上传头像
- 超级计算机硬盘有多大,天河二号超级计算机在存储方面用的是什么硬盘?
- 庖丁解牛Linux内核分析慕课课程
- Python基础知识(2): 字符串
- 西方数学史上的瑰宝:欧几里得与《几何原本》
- Jsoup学习 JAVA爬虫爬取美女网站 JAVA爬虫爬取美图网站 爬虫
- OSG+VS2010+win7环境搭建