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. Windows 程序安装与更新方案: Clowd.Squirrel

    我的Notion Clowd.Squirrel Squirrel.Windows 是一组工具和适用于.Net的库,用于管理 Desktop Windows 应用程序的安装和更新. Squirrel.W ...

  2. 个人冲刺(一)——体温上报app(二阶段)

    冲刺任务:完成app登录和注册页面的布局 activity_register.xml <?xml version="1.0" encoding="utf-8&quo ...

  3. Redis 全局通用命令整理

    转载请注明出处: 1.查看所有键 keys * 该命令会存在线程阻塞问题,keys 命令也可以通过正则匹配获取存在的缓存数据 2.查看键总数 dbsize dbsize命令会返回当前数据库中键的总数. ...

  4. 基于Python的渗透测试信息收集系统的设计和实现

    信息收集系统的设计和实现 渗透测试是保卫网络安全的一种有效且必要的技术手段,而渗透测试的本质就是信息收集,信息搜集整理可为后续的情报跟进提供强大的保证,目标资产信息搜集的广度,决定渗透过程的复杂程度, ...

  5. 如何用 UDP 实现可靠传输?

    作者:小林coding 计算机八股文刷题网站:https://xiaolincoding.com 大家好,我是小林. 我记得之前在群里看到,有位读者字节一面的时候被问到:「如何基于 UDP 协议实现可 ...

  6. Java 多线程共享模型之管程(上)

    主线程与守护线程 默认情况下,Java 进程需要等待所有线程都运行结束,才会结束.有一种特殊的线程叫做守护线程,只要其它非守护线程运行结束了,即使守护线程的代码没有执行完,也会强制结束. packag ...

  7. LVGL库入门教程04-样式

    LVGL样式 LVGL样式概述 创建样式 在 LVGL 中,样式都是以对象的方式存在,一个对象可以描述一种样式.每个控件都可以独立添加样式,创建的样式之间互不影响. 可以使用 lv_style_t 类 ...

  8. SAP Picture Control(图片加载)

    Screen display 效果 源代码 program sap_picture_demo. set screen 200. TYPE-POOLS cndp. ******************* ...

  9. 数据库系列:MySQL索引优化总结(综合版)

    1 背景 作为一个常年在一线带组的Owner以及老面试官,我们面试的目标基本都是一线的开发人员.从服务端这个技术栈出发,问题的范围主要还是围绕开发语言(Java.Go)等核心知识点.数据库技术.缓存技 ...

  10. Git代码提交报错 (Your branch is up to date with 'origin/master)

    一.前言 今天码云上提交自己本地的一个SpringBoot+Vue的小项目,由于前端代码提交第一次时候提交码云上文件夹下为空,于是自己将本地代码复制到码云拉取下来代码文件夹下,然而git add . ...