Veux mapState、mapGetters、mapActions、mapMutations && Vuex命名空间
1 # 一、四个map方法的使用
2 # 1.mapState方法:用于帮助我们映射state中的数据为计算属性
3 computed:{
4 // sum(){
5 // return this.$store.state.sum;
6 // },
7 // 借助mapState生成计算数据 对象写法
8 ...mapState({sum: 'sum'}), // 代替上面的sum()
9 // 借助mapState生成计算数据 数组写法
10 ...mapState(['sum']), // 代替上面的sum()
11 },
12 # 2.mapGetters方法:用于帮助我们映射getters中的数据为计算属性
13 computed:{
14 // bigSum(){
15 // return this.$store.getters.bigSum;
16 // },
17 // 借助mapState生成计算属性 对象写法
18 ...mapGetters({bigSum: 'bigSum'}), // 代替上面的bigSum()
19 // 借助mapState生成计算属性 数组写法
20 ...mapGetters(['bigSum']), // 代替上面的bigSum()
21 },
22 # 3.mapActions方法:用于帮助我们生成actions对话中的方法
23 methods:{
24 // invrementOdd(){
25 // this.$store.dispatch('jiaOdd', this.n);
26 // },
27 // invrementWait(){
28 // this.$store.dispatch('jiaWait', this.n);
29 // },
30 // 靠mapActions生成,代替 invrementOdd invrementWait函数 对象写法
31 ...mapActions({invrementOdd: 'jiaOdd', invrementWait: 'jiaWait'})
32 // 靠mapActions生成,代替 invrementOdd invrementWait函数 数组写法
33 ...mapActions(['jiaOdd', 'jiaWait'])
34 },
35 # 4.mapMutations方法:用于帮助我们生成与mutations对话的方法,即:包含$store.commit(xxx)的函数
36 methods:{
37 // invrement(){
38 // // console.log('@', this);
39 // this.$store.commit('JIA', this.n);
40 // },
41 // decrement(){
42 // this.$store.commit('JIAN', this.n);
43 // },
44 // 靠mapMutations生成,代替invrement decrement 对象形式
45 ...mapMutations({invrement:'JIA', decrement: 'JIAN'}),
46 // 靠mapMutations生成,代替invrement decrement 数组简写
47 ...mapMutations(['JIA', 'JIAN'])
48 },
49 # 5.备注:mapActions与Mutations使用时,若要传递参数,需要在模板中绑定事件时传递好参数,否则会默认传一个事件对象。
50
51
52 # 二、模块化+命名空间
53 # 1.目的:让代码更好维护,让更多种数据分类更加明确。
54 # 2.修改store.js
55 const countAbout = {
56 namespaced: true, // 开启命名空间
57 state:{x:1},
58 mutations:{...},
59 actions:{...},
60 getters:{
61 bigSum(state){
62 return state.sum * 10;
63 }
64 }
65 }
66 const personAbout = {
67 namespaced: true,
68 state:{...},
69 mutations: {...},
70 actions: {...}
71 }
72 const store = new Vuex.Store({
73 models:{
74 countAbout,
75 personAbout
76 }
77 })
78 # 3.开启命名空间后,组件中读取state数据:
79 方式一:自己直接读取
80 this.$stroe.state.personAbout.list
81 方式二:借助mapState读取
82 ...mapState('countAbout',['sum', 'school','subject'])
83 # 4.开启命名空间后,组件中读取getters数据
84 # 方式一:自己直接读取
85 this.$store.getters['personAbout/firstPersonName']
86 # 方式二:借助mapGetters读取
87 ...mapGetters('countAbout',[bigSum])
88 # 5.开启命名空间后,组件中调用dispatch
89 # 方式一:自己直接dispatch
90 this.$store.dispatch('personAbout/addPersonWang', person)
91 # 方式二:借助mapActions:
92 ...mapActions('countAbout',{incrementOdd:'jiaOdd',incrementWait:'jiaWait'})
93 # 6.开启命名空间后,组件中调用commit
94 # 方式一:自己直接commit
95 this.$store.commit('personAbout/ADD_PERSON', person)
96 # 方式二:借助mapMutations
97 ...mapMutations('countAbout',{increment:'JIA',decrement:'JIAN'})
Veux mapState、mapGetters、mapActions、mapMutations && Vuex命名空间的更多相关文章
- vuex之 mapState, mapGetters, mapActions, mapMutations 的使用
一.介绍 vuex里面的四大金刚:State, Mutations,Actions,Getters (上次记得关于vuex笔记 http://www.cnblogs.com/adouwt/p/8283 ...
- vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations
1.导入辅助函数 导入mapState可以调用vuex中state的数据 导入mapMutations可以调用vuex中mutations的方法 四个辅助函数 各自对应自己在vuex上的自己 2.ma ...
- vuex里mapState,mapGetters使用详解
这次给大家带来vuex里mapState,mapGetters使用详解,vuex里mapState,mapGetters使用的注意事项有哪些,下面就是实战案例,一起来看一下. 一.介绍 vuex里面的 ...
- Vuex mapGetters,mapActions
一.基本用法 1. 初始化并创建一个项目 ? 1 2 3 vue init webpack-simple vuex-demo cd vuex-demo npm install 2. 安装 vuex ? ...
- vuex 的使用 mapState, mapGetters, mapMutations, mapActions
state => 基本数据getters => 从基本数据派生的数据mutations => 提交更改数据的方法,同步!actions => 像一个装饰器,包裹mutation ...
- 理解Vuex的辅助函数mapState, mapActions, mapMutations用法
在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 # 工程名 | |--- dist # 打包后生成的目录文件 | |--- node_modules # 所有的 ...
- [转]理解Vuex的辅助函数mapState, mapActions, mapMutations用法
原文地址:https://www.cnblogs.com/tugenhua0707/p/9794423.html 在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 ...
- Vue 状态管理之vuex && {mapState,mapGetters}
1 # 一.理解vuex 2 1.概念:专门在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读写),也是一种组件间通信的方式,且适用于任意组件间 ...
- mapState ,mapGetters ,mapMutations,mapActions
参考 http://www.imooc.com/article/14741
随机推荐
- 关于Spring中的useSuffixPatternMatch
背景 spring-boot的版本是2.1.4.RELEASE,spring的版本是5.1.6.RELEASE 一个例子如下: @Configuration @Import(WebMvcAutoCon ...
- 6. ZigZag Conversion - LeetCode
Question 6. ZigZag Conversion Solution 题目大意:将字符串按Z字型排列,然后再一行一行按字符输出 思路:按题目中的第一个例子,画出如下图,通过n的不同值,可以找出 ...
- 278. First Bad Version - LeetCode
Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...
- 学习Java的第十五天——数学运算
学习内容:数学运算 1.三角函数运算 代码实现: public class 三角函数运算 { public static void main(String[] args) { // TODO 自动生成 ...
- 【FineBI】FineBI连接阿里云mysql教程
因为某些原因需要查看数据信息,之前连接成功一次,今天软件更新了以后发现连接信息丢. 又重新折腾了一下. 主要有2个地方: 1.查看阿里云数据库外网连接地址:打开云数据库RDS-实例列表-管理-数据库连 ...
- 走进Linux的世界
开源软件Linux的起源: Linux--操作系统. Linux,1991年Linux之父林纳斯 本纳第克特 托瓦兹,创建了Linux操作系统内核(开源). Linux的发行版和RHCE 1.Linu ...
- 【SNOI2017 DAY1】炸弹
题意:P5024 思路:首先\(O(n^2)\)向能炸到的点连边,所以能到达的点的个数就是能到达的点的个数.然后显然要缩点+拓扑排序(我写的记搜). 然后再写一个线段树优化建图. 然后就WA了,我想了 ...
- 一款开源的跨平台实时web应用框架——DotNetify
今天给大家介绍一个开源的轻量级跨平台实时HTML+C#.NET Web应用程序开发框架--DotNetify,允许你在C#.NET后端上创建具有React.React Native.Vue或Blazo ...
- 线上问题定位利器 jprofiler
1.导出dump windows: jps -l 查看Java进行 jmap -dump:format=b,file=webapi.hprof 20840 查看进程,根据进程号导出hprof文件 ...
- 1.Shell编程循环语句(if 、while、 until)
循环语句 for循环语句 读取不同的变量值,用来逐个执行同一组命令 格式: for 变量名 in 取值列表 do 命令序列 done 示例:批量创建用户并设置密码 [root@localhost da ...