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. php url路由伪静态

    RewriteEngine on RewriteBase /RewriteRule ^([a-zA-Z]{1,})/([a-zA-Z]{1,})$ webim2/operator/users.php? ...

  2. oc 把view添加到rootcontrollerview控制的view

    在当前活跃的window 添加一个view [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] add ...

  3. 【HttpClient4.5中文教程】【第一章 :基础】1.1运行请求(二)

    很多其它HttpClient4.5中文教程请查看:点击打开链接 ==================================================================== ...

  4. PAT 1001. A+B Format(水题)

    #include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b ...

  5. lua学习笔记(五)

    语句     赋值         多重赋值         a, b, c, d = 1, 2, 3, 4         a, b, c = 1, 2         assert(c == ni ...

  6. java中已经排序的列表中插入新值

    static List<Integer> insertSortedList(){ List<Integer> nums = new ArrayList<Integer&g ...

  7. Warning: (3719, “‘utf8’ is currently an alias for the character set UTF8MB3, which will be replaced by UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.”)

    [1]本地版本 Mysql 8.0.12 创建表SQL: DROP TABLE IF EXISTS students; CREATE TABLE `students` ( `sId` ) UNSIGN ...

  8. 图论——Dijkstra+prim算法涉及到的优先队列(二叉堆)

    [0]README 0.1)为什么有这篇文章?因为 Dijkstra算法的优先队列实现 涉及到了一种新的数据结构,即优先队列(二叉堆)的操作需要更改以适应这种新的数据结构,我们暂且吧它定义为Dista ...

  9. iOS图片无损拉伸

    一张图片如果放大的话一般情况下会失真,如果该图片是规则的,比如这个聊天气泡,可以用如下代码来设置 UIImage *rightImg = [UIImage imageNamed:@"Sen ...

  10. BI测试

    BI概念: 商业智能(Business Intelligence 简称BI),指数据仓库相关技术与应用的通称.指利用各种智能技术,来提升企业的商业竞争力.是帮助企业更好地利用数据提高决策质量的技术,包 ...