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: ...
随机推荐
- right join 和left join 的区别
SQL 数据库 right join 和left join 的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中 ...
- Nginx日志常见时间变量解析
$request_time 官方解释:request processing time in seconds with a milliseconds resolution; time elapsed b ...
- io流追加到一个文件中信息比如日志
package com.yh.day02.arrays; import java.io.File;import java.io.FileInputStream;import java.io.FileN ...
- MyBatis中@MapKey使用详解
MyBatis中@MapKey使用详解我们在上一篇文章中讲到在Select返回类型中是返回Map时,是对方法中是否存在注解@MapKey,这个注解我也是第一次看到,当时我也以为是纯粹的返回单个数据对象 ...
- lombok深入实践
官网视频 官网地址:https://projectlombok.org 官网的首页视频演示在eclipse中如何使用Lombok; Project Lombok is a java library t ...
- Django之web框架原理
Web框架原理 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 先写一个 原始的web框架 imp ...
- Elasticsearch 6.x版本全文检索学习之聚合分析入门
1.什么是聚合分析? 答:聚合分析,英文为Aggregation,是es除搜索功能外提供的针对es数据做统计分析的功能.特点如下所示: a.功能丰富,提供Bucket.Metric.Pipeline等 ...
- shell 中长命令的换行处理
考察下面的脚本: emcc -o ./dist/test.html --shell-file ./tmp.html --source-map-base dist -O3 -g4 --source-ma ...
- Sqlite-net 修改版 支持中文和CodeFirst技术
最近, 做的一个windows 桌面WPF程序, 需要数据库支持.尝试了 sql server 的开发版,使用EF , 效率太低.后来采用sqlite数据库,中间踩坑无数.但最终完美的解决了这些问题. ...
- 目前为止最简洁的C#文件夹Copy代码,不接受反驳
private static void CopyEntireDir(string sourcePath, string destPath) { foreach (string dirPath in D ...