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. sa-token 配置 CORS

    return new SaServletFilter() ... .setBeforeAuth(r -> { // 前置函数,在认证函数每次执行前执行 // 设置一些安全响应头之类的玩意 SaH ...

  2. 138_Power BI&Power Pivot特殊半累加度量

    博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 半累加度量(semi-additive measure),在DAX建模分析的时候经常遇见:应用场景诸如银行存款. ...

  3. 111_Power Pivot 24小时维度:累计、同比、环比相关

    博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 今天有朋友讨论怎么做每天24小时维度的工作量计算(运营类企业,每天24小时都在运营)需求如下: 1.从0时到23 ...

  4. 「POI2012」井 Well

    description 你要挖井,\(n\)个地面的高度可视为\(h_i\),每次操作你可以将一个\(h_i-1\),你最多可执行\(m\)次操作. 你要任选其中一个\(h_i\)挖到\(0\),问你 ...

  5. 关于spring整合mybatis

    第一步导入依赖 <dependencies> <dependency> <groupId>org.mybatis</groupId> <artif ...

  6. git 无法拉取最新代码

    删除本地文件后,想从远程仓库中重新新Pull最新代码,但是执行了git pull origin develop 命令后始终无法拉取下来 提示 Already up-to-date. 原因:当前本地库处 ...

  7. MVC - MVC的工作流程

    MVC 是Model-View-Controller的简写."Model" 代表的是应用的业务逻辑(通过JavaBean,EJB组件实现), "View" 是应 ...

  8. python和numpy中sum()函数的异同

    转载:https://blog.csdn.net/amuchena/article/details/89060798和https://www.runoob.com/python/python-func ...

  9. Git使用 - 忽略特定文件 - gitignore

    1. 背景 2. 创建.gitignore 文件 3. 文件内容样式 4. exclude文件 5. gitignore 文件模板 6. 参考文档 1. 背景 前提知识:在工作目录下的每一个文件都不外 ...

  10. Spire.Office激活

    更新记录: 2022年5月28日 初始代码便于复用 注意:最多支持到:E-ICEBLUE Spire.Office Platinum v6.10.3 引入命名空间: using Spire.Licen ...