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命名空间的更多相关文章

  1. vuex之 mapState, mapGetters, mapActions, mapMutations 的使用

    一.介绍 vuex里面的四大金刚:State, Mutations,Actions,Getters (上次记得关于vuex笔记 http://www.cnblogs.com/adouwt/p/8283 ...

  2. vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations

    1.导入辅助函数 导入mapState可以调用vuex中state的数据 导入mapMutations可以调用vuex中mutations的方法 四个辅助函数 各自对应自己在vuex上的自己 2.ma ...

  3. vuex里mapState,mapGetters使用详解

    这次给大家带来vuex里mapState,mapGetters使用详解,vuex里mapState,mapGetters使用的注意事项有哪些,下面就是实战案例,一起来看一下. 一.介绍 vuex里面的 ...

  4. Vuex mapGetters,mapActions

    一.基本用法 1. 初始化并创建一个项目 ? 1 2 3 vue init webpack-simple vuex-demo cd vuex-demo npm install 2. 安装 vuex ? ...

  5. vuex 的使用 mapState, mapGetters, mapMutations, mapActions

    state => 基本数据getters => 从基本数据派生的数据mutations => 提交更改数据的方法,同步!actions => 像一个装饰器,包裹mutation ...

  6. 理解Vuex的辅助函数mapState, mapActions, mapMutations用法

    在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 # 工程名 | |--- dist # 打包后生成的目录文件 | |--- node_modules # 所有的 ...

  7. [转]理解Vuex的辅助函数mapState, mapActions, mapMutations用法

    原文地址:https://www.cnblogs.com/tugenhua0707/p/9794423.html 在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 ...

  8. Vue 状态管理之vuex && {mapState,mapGetters}

    1 # 一.理解vuex 2 1.概念:专门在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读写),也是一种组件间通信的方式,且适用于任意组件间 ...

  9. mapState ,mapGetters ,mapMutations,mapActions

    参考 http://www.imooc.com/article/14741

随机推荐

  1. Vue的computed和watch的使用和区别

    一.computed: 模板内表达式非常便利,可用于简单计算,当模板内放入太多的逻辑时,模板会过重且难以维护:可以使用computed替代 计算属性是基于它们的响应式依赖进行缓存的,当依赖的响应式数据 ...

  2. idea maven 依赖还原不上的问题 method <init>()V not found

    问题 还原项目依赖的时候报错: java.lang.RuntimeException: org.codehaus.plexus.component.repository.exception.Compo ...

  3. linux篇-修改mysql数据库密码

    总是忘记,每次都要查文档,背背背 方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = passw ...

  4. 好客租房14-在jsx中使用javascript表达式的注意点

    注意点 单大括号中可以使用任意的表达式 jsx自身也是js表达式 注意:js中的对是一个例外 写在style样式中 //导入react     import React from "reac ...

  5. springcloud 断路器

    https://www.jb51.net/article/138572.htm 参考资料: http://www.cnblogs.com/ulysses-you/p/7281662.html http ...

  6. Koa系框架(egg/cabloy)如何获取微信支付回调请求中的xml参数

    背景 在Koa系框架(如EggJS)中进行微信支付开发时,遇到一个问题:微信支付平台会发送一个回调请求,通知支付订单的处理结果.该请求传入的参数是xml格式,而Koa中间件koa-bodyparser ...

  7. Node.js精进(1)——模块化

    模块化是一种将软件功能抽离成独立.可交互的软件设计技术,能促进大型应用程序和系统的构建. Node.js内置了两种模块系统,分别是默认的CommonJS模块和浏览器所支持的ECMAScript模块. ...

  8. (数据科学学习手札139)geopandas 0.11版本重要新特性一览

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,就在几天前,geopandas ...

  9. this关键字、static关键字、方法的调用

    1.带有static关键字的方法,不可使用this关键字.因为其调用方法为类名.方法名(建议这种方式,调用不需要对象的参与),不存在对象. 2.实例方法调用必须有对象的存在,先创建对象,通过引用.的方 ...

  10. 女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!

    1. 背景 MySQL实现事务.崩溃恢复.集群的主从复制,底层都离不开日志,所以日志是MySQL的精华所在.只有了解MySQL日志,才算是彻底搞懂MySQL. 今天一灯就带你深入浅出的学习MySQL的 ...