mutation

作用: 更改state的状态

说明: 每个mutation对象都有字符串类型(type)与回调函数,在回调函数内进行状态修改,回调函数的第一个参数为state

eg:

mutations: {
changeMainOption (state, index) {
state.preMainOption= index;
},
changeShade(state, type){
state.mainShade= type;
},
changeprePerser(state, _index){
state.prePerser= _index;
}
}

调用方式:

1.载荷风格

this.$store.commit('changeMainOption', 1)

//index为1

2.对象风格

this.$store.commit({type: 'changeMainOption', anyName: 1})

//index为{type: 'changeMainOption', anyName: 1}

注意:

mutation必须是同步函数,

mutation第二个参数在载荷风格时为commit的第二个参数,对象风格时为commit的对象参数。

action

功能: 提交mutation,可包含异步操作

说明: action函数接收一个与store有相同属性方法的实例context来提交mutation

eg:这

actions: {
toChangeMainOption (context){
      setTimeout(()=> {context.commit('changeMainOption', 1)}, 1000)
    },
toChangeMainOption2 ({commit}){ //es6参数解构写法
commit('changeMainOption', 2)
}
}

调用方式:

1.载荷风格

this.$store.dispatch(''toChangeMainOption", {val: 1})

2.对象风格

this.$store.dispatch({type: ''toChangeMainOption", val: 1})

mutation与action的更多相关文章

  1. vuex mutation,action理解

    1. 在store中分别注册mutation和action,action中用commit同步调用mutation来执行修改state,但是在组件中则使用dispatch异步调用action 2. 通俗 ...

  2. vuex2.0 基本使用(2) --- mutation 和 action

    我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...

  3. 【14】vuex2.0 之 mutation 和 action

    我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...

  4. (转)vuex2.0 基本使用(2) --- mutation 和 action

    我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...

  5. Vuex 的使用 State Mutation Getter Action

    import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); /*1.state在vuex中用于存储数据*/ var state={ cou ...

  6. vuex中mutation和action的详细区别

    const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } ...

  7. 【vuex】mutation和action的区别

    const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } ...

  8. 06-vue项目02:vuex、Mutation、Action、ElementUI、axios

    1.Vuex 1.为什么使用VueX data从最上面的组件,一层层往下传值,一层层的验证 Vue单向数据流 “中央空调“,代理 VueX 解决数据 传值.. 2.Vuex介绍与安装 (1)Vuex官 ...

  9. 关于Vue.js 2.0 的 Vuex 2.0,你需要更新的知识库

    应用结构 实际上,Vuex 在怎么组织你的代码结构上面没有任何限制,相反,它强制规定了一系列高级的原则: 应用级的状态集中放在 store 中. 改变状态的唯一方式是提交mutations,这是个同步 ...

随机推荐

  1. ubuntu 安装时出错 sudo apt-get update Reading package lists… Error

    安装过程出错 首先出现问题sudo apt-get updateReading package lists… Error!E: Encountered a section with no Packag ...

  2. view无限旋转

    - (void) showRefreshAnimation { [UIView animateWithDuration: options:UIViewAnimationOptionCurveLinea ...

  3. angularjs中的$q

    先说说什么是Promise,什么是$q吧.Promise是一种异步处理模式,有很多的实现方式,比如著名的Kris Kwal's Q还有JQuery的Deffered. 什么是Promise 以前了解过 ...

  4. IP地址加时间戳加3位随机数

    工作中经常用到时间戳加上3位随机数获得唯一流水号,下面是代码~ package com.pb.viewer.filename; import java.text.SimpleDateFormat; i ...

  5. File 的基本操作

    package xinhuiji_day07; import java.io.File;import java.io.IOException; public class FileTest { /**  ...

  6. webview长按保存图片

    private String imgurl = ""; /***     * 功能:长按图片保存到手机     */    @Override    public void onC ...

  7. 【elasticsearch】安装合集

    [elasticsearch](1)centos7 使用yum安装elasticsearch 2.X [elasticsearch](2)centos7 超简单安装elasticsearch 的监控. ...

  8. 【JMeter4.0学习(八)】之断言

    目录 响应断言 一.响应断言 1.添加线程组 2.添加HTTP请求默认值 3.添加HTTP请求1 4.先运行“HTTP请求1”,查看结果树的“取样器结果.请求.响应数据” ①取样器结果 ②请求 ③响应 ...

  9. Unable to VNC onto Centos server remotely

    用的好好的vncserver 突然遇到这个错误: [vnc@localhost ~]$ sudo systemctl status vncserver@:1.service -l● vncserver ...

  10. ubuntu下搭建的lamp环境新建站点

    这几天刚装了一个ubuntu 16.04桌面版,总之来来回回几遍才基本把环境搭建好,本来用apt-get搭建,结果不知道什么原因16.04版不支持装php5 ,提示源放弃了php5版本,不得不使用ph ...