vuex 的使用 mapState, mapGetters, mapMutations, mapActions
state => 基本数据
getters => 从基本数据派生的数据
mutations => 提交更改数据的方法,同步!
actions => 像一个装饰器,包裹mutations,使之可以异步。
modules => 模块化Vuex
export default{
state: {
count:5
},
getters: {
// 单个参数
countDouble: function(state){
return state.count * 2
},
// 两个参数
countDoubleAndDouble: function(state, getters) {
return getters.countDouble * 2
}
},
mutations: {
//无提交荷载
increment(state) {
state.count++
}
//提交荷载
incrementN(state, obj) {
state.count += obj.n
}
},
actions: {
increment (context) {
setInterval(function(){
context.commit('increment')
}, 1000)
}
}
<template>
<div class="p-genDan">
<p>{{ $store.state.count }}</p><!--state第一种使用方法,直接使用-->
<p>{{ count }}</p> <!--state第二种方式-->
</div>
</template>
<script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
export default {
name: 'genDan',
data() {
return {};
},
beforeCreate() {},
created() {},
beforeMount() {},
mounted() {
//mutations调用 第一种形式
//无提交荷载
this.$store.commit('increment')
//提交荷载
this.$store.commit('incrementN', {n: 100})
//对象风格的提交方式
this.$store.commit({ type: 'incrementN', n: 10}) //mutations调用 第二种形式
this.increment();
this.add(); //Action 通过 store.dispatch 方法触发:
this.$store.dispatch('increment')
// 以载荷形式分发
this.$store.dispatch('incrementN', {n: 10})
// 以对象形式分发
this.$store.dispatch({type: 'incrementN',n: 10})
//Action通过 mapActions
this.incrementN();
},
computed: {
...mapState({
tabView:state => state.selfCenter.moneyManageTabView, //调用子模块selfCenter的state
count: state => state.count, //state第二种使用方法
// 传字符串参数 'count' 等同于 `state => state.count`
countAlias: 'count',
// 为了能够使用 `this` 获取局部状态,必须使用常规函数
countPlusLocalState (state) { //state的重新筛选,当前组件有效
return state.count + 10
}
}),
//getters调用第一种调用方式
countDouble: function(){
return this.$store.getters.countDouble
},
countDoubleAndDouble: function(){
return this.$store.getters.countDoubleAndDouble
},
//getters第二种调用方式
//使用对象展开运算符将 getters 混入 computed 对象中
...mapGetters([
'countDouble',
'countDoubleAndDouble',
//如果你想将一个 getter 属性另取一个名字,使用对象形式: 映射 this.double 为 store.getters.countDouble
double: 'countDouble'
//..
]),
...mapGetters('selfCenter',['oneWeekBetTotal','oneWeekBetEcharts']), //调用子模块selfCenter中getters
},
methods: { //mutations调用 第二种形式
...mapMutations('selfCenter',['changeMoneyManageTabView']), //调用子模块selfCenter
...mapMutations([
'increment' // 映射 this.increment() 为 this.$store.commit('increment')
]),
...mapMutations({
add: 'increment' // 映射 this.add() 为 this.$store.commit('increment')
}), //mapActions
...mapActions([
'incrementN' //映射 this.incrementN() 为 this.$store.dispatch('incrementN')
]),
...mapActions({
add: 'incrementN' //映射 this.add() 为 this.$store.dispatch('incrementN')
})
},
beforeUpdate() {},
updated() {},
beforeDestroy() {},
destroyed() {}
};
</script>
vuex 的使用 mapState, mapGetters, mapMutations, mapActions的更多相关文章
- mapState ,mapGetters ,mapMutations,mapActions
参考 http://www.imooc.com/article/14741
- vuex mapState、mapGetters、mapActions、mapMutations的使用
例子: index.js import Vue from 'vue' import Vuex from 'vuex' import mutations from './mutations' impor ...
- vuex之 mapState, mapGetters, mapActions, mapMutations 的使用
一.介绍 vuex里面的四大金刚:State, Mutations,Actions,Getters (上次记得关于vuex笔记 http://www.cnblogs.com/adouwt/p/8283 ...
- vue:vuex中mapState、mapGetters、mapActions辅助函数及Module的使用
一.普通store中使用mapState.mapGetters辅助函数: 在src目录下建立store文件夹: index.js如下: import Vue from 'vue'; import ...
- 关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(四)-----mapActions
介绍mapActions辅助函数: Action提交的是Mutation,不能够直接修改state中的状态,而Mutations是可以直接修改state中状态的:Action是支持异步操作的,而Mut ...
- vuex里mapState,mapGetters使用详解
这次给大家带来vuex里mapState,mapGetters使用详解,vuex里mapState,mapGetters使用的注意事项有哪些,下面就是实战案例,一起来看一下. 一.介绍 vuex里面的 ...
- vuex中mapState、mapMutations、mapAction的理解
当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余.为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性. // 在单独构建的版本中辅助函数为 Vue ...
- Vue 状态管理之vuex && {mapState,mapGetters}
1 # 一.理解vuex 2 1.概念:专门在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读写),也是一种组件间通信的方式,且适用于任意组件间 ...
- vuex2中使用mapMutations/mapActions报错解决方法 BabelLoaderError: SyntaxError: Unexpected token
在尝鲜vuex2时,发现vuex2增加了 mapGetters 和 mapActions 的方法,借助stage2的 Object Rest Operator 特性,可以写出下面代码:methods: ...
随机推荐
- Dynamics 365利用Web API对视图进行查询
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- zipalign的使用
zipalign 是一个存档对齐工具,可为Android应用程序(.apk)文件提供重要的优化.目的是确保所有未压缩数据以相对于文件开头的特定对齐开始.具体来说,它会导致.apk中的所有未压缩数据(如 ...
- Android项目实战之高仿网易云音乐创建项目和配置
这一节我们来讲解创建项目:说道大家可能就会说了,创建项目还有谁不会啊,还需要讲吗,别急听我慢慢到来,肯定有你不知道的. 使用项目Android Studio创建项目我们这里就不讲解了,主要是讲解如何配 ...
- 使用jeecg-boot心得
使用jeecg-boot心得: Jeect-boot,采用主流最新的开发技术,是个强大的快速开发平台. 刚开始发现jeecg-boot时便对其精致美观的页面深深的迷住了.下载项目运行发现其中也有想要的 ...
- dependencyManagement
maven中的继承是在子类工程中加入父类的groupId,artifactId,version并用parent标签囊括 depenentManagement标签作用: 当父类的pom.xml中没有de ...
- Attention 和self-attention
1.Attention 最先出自于Bengio团队一篇论文:NEURAL MACHINE TRANSLATION BY JOINTLY LEARNING TO ALIGN AND TRANSLATE ...
- [考试反思]1110csp-s模拟测试108:消遣
是套废题.T1题面错了,T2细节多而暴力>部分分,T3题目错了. T1:打表 题面应该是输出差值期望而不是答案值期望. 看到题目,果断打表. 答案就是所有值差之和除2的k次方. #include ...
- 百度云盘资源 for MAC 第三方工具不限速下载
相信大家都比较困惑,百度网盘客户端限速后一般只有几十K的下载速度,Windows有百度网盘破解版,但MAC的破解版似乎不存在,要提速的话,一般的做法是开超级会员(27元/月),身为程序员的我们,是不是 ...
- Zabbix-(七)分布式监控
Zabbix-(七)分布式监控 一.前言 Zabbix提供了一套分布式监控的方案,即使用Zabbix Proxy,本文记录使用Zabbix Proxy进行分布式监控. 官方所述Proxy的使用场景如下 ...
- 手把手教你制作Jlink-OB调试器(含原理图、PCB、外壳、固件)
前言 好久没更新博客和公众号了,感谢大家还没取关哈,好吧,我承认是我太懒了,今天分享一个福利! 趁着前段时间嘉立创和捷配打价格战,一天之内,多次降价,看着真是热闹.捷配降到最低3元一款,而嘉立创降到最 ...