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的更多相关文章

  1. mapState ,mapGetters ,mapMutations,mapActions

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

  2. vuex mapState、mapGetters、mapActions、mapMutations的使用

    例子: index.js import Vue from 'vue' import Vuex from 'vuex' import mutations from './mutations' impor ...

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

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

  4. vue:vuex中mapState、mapGetters、mapActions辅助函数及Module的使用

    一.普通store中使用mapState.mapGetters辅助函数: 在src目录下建立store文件夹: ​ index.js如下: import Vue from 'vue'; import ...

  5. 关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(四)-----mapActions

    介绍mapActions辅助函数: Action提交的是Mutation,不能够直接修改state中的状态,而Mutations是可以直接修改state中状态的:Action是支持异步操作的,而Mut ...

  6. vuex里mapState,mapGetters使用详解

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

  7. vuex中mapState、mapMutations、mapAction的理解

    当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余.为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性. // 在单独构建的版本中辅助函数为 Vue ...

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

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

  9. vuex2中使用mapMutations/mapActions报错解决方法 BabelLoaderError: SyntaxError: Unexpected token

    在尝鲜vuex2时,发现vuex2增加了 mapGetters 和 mapActions 的方法,借助stage2的 Object Rest Operator 特性,可以写出下面代码:methods: ...

随机推荐

  1. iOS-关于自定义分段选择器的一些小事(Segmented)

    系统自带的分段选择就是 UISegmentedControl ,也有一些大佬自定义的 Segmented ,比如Git上的 HMSegmentedControl ,我以前最初的项目中,也有用到过,如果 ...

  2. (python pip安装第三方库超时问题(raise ReadTimeoutErrorself._pool, None, 'Read timed out.')

    (python pip安装第三方库超时问题(raise ReadTimeoutErrorself._pool, None, ‘Read timed out.’)pip工具安装百度经验链接: pip安装 ...

  3. PHP 生成 UUID 函数

    Generate name based md5 UUID (version 3) /** * Generate name based md5 UUID (version 3). * @example ...

  4. Mysql基本注入

    实验环境:墨者学院Mysql手工注入漏洞测试靶场 后台源码没有进行任何字符过滤. 首先进入靶场环境 先用admin登陆试试 果然不行,这时看到用户登录下方有一个停机维护通知,点进去瞅瞅 看到这里链接上 ...

  5. 各种数和各种反演(所谓FFT的前置知识?)

    每次问NC做多项式的题需要什么知识点. 各种数. 各种反演. 多项式全家桶. 然后我就一个一个地学知识点.然而还差好多,学到后面的前面的已经忘了(可能是我太菜吧不是谁都是NC啊) 然后发现每个知识点基 ...

  6. java之包装类

    针对八种基本数据类型定义相应的引用类型--包装类: 有了类的特点,接可以调用类中的方法: 基本数据类型 包装类 boolean Bollean byte Byte short Short int In ...

  7. SpringSession header/cookie/attribute存放 session id

    SpringSession header/cookie/attribute存放 SessionID(死磕) 疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长 ...

  8. rmi与rpc的区别

    这里简单说一下RMI和RPC的区别. 什么是RMI RMI(Remote Method Invocation,远程方法调用),能够让在客户端Java虚拟机上的对象像调用本地对象一样调用服务端Java虚 ...

  9. SpringCloud微服务(07):Zipkin组件,实现请求链路追踪

    本文源码:GitHub·点这里 || GitEE·点这里 一.链路追踪简介 1.Sleuth组件简介 Sleuth是SpringCloud微服务系统中的一个组件,实现了链路追踪解决方案.可以定位一个请 ...

  10. 图数据库 Nebula Graph 的安装部署

    Nebula Graph:一个开源的分布式图数据库.作为唯一能够存储万亿个带属性的节点和边的在线图数据库,Nebula Graph 不仅能够在高并发场景下满足毫秒级的低时延查询要求,还能够实现服务高可 ...