你可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。

import Vue from 'vue';
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App';
import router from './router';
import Vuex from 'vuex'; Vue.config.productionTip = false;
Vue.use(Vuex);
Vue.use(Element) //vuex的配置
//注意Store是大写
const store = new Vuex.Store({
//数据保存
state: {
show: false,
count: 0,
list: [1, 5, 8, 10, 30, 50]
},
mutations: {
increase(state, n = 1) {
state.count +=
n;
},

decrease(state, n = 1) {
state.count -= n;
},
switch_dialog(state) { // 这里的state对应着上面这个state
state.show = state.show ? false : true
// 你还可以在这里执行其他的操作改变state
}
},
getters: {
filteredList: state => {
return state.list.filter(item => item < 31);
}
},
actions: {
asyncIncrease(context) {
context.commit('increase');
},
switch_dialog123(context) { // 这里的context和我们使用的$store拥有相同的对象和方法
context.commit('switch_dialog')
// 你还可以在这里触发其他的mutations方法 }
}
}); /* eslint-disable no-new */
new Vue({
el: '#app',
router,
//使用vuex
store: store,
render: h => h(App),
});
<template>
<div>
{{count}}
<button @click="handleIncrease">+5</button>
<button @click="handleDecrease">-5</button> {{filteredList}}
<button @click="handleRouter">跳转到 HelloWorld3</button>
<button @click="showRouter">展示路由</button>
</div>
</template> <script>
import { mapState } from 'vuex'
import { mapGetters } from 'vuex'
import { mapMutations } from 'vuex'
export default {
name: 'HelloWorld2',
computed: {
// count(){
// return this.$store.state.count;
// },
// filteredList() {
// return this.$store.getters.filteredList;
// },
...mapState({
count: state => state.count
}),
// 使用对象展开运算符将 getter 混入 computed 对象中
...mapGetters([
'filteredList'
])
},
methods: {
handleIncrease() {
// this.$store.commit('increase', 5);
this.increase();
},
handleDecrease() {
this.$store.commit('decrease', 5);
},
handleAsyncIncrease() {
this.$store.dispatch('asyncIncrease');
},
handleRouter() {
this.$router.push('/HelloWorld3');
},
showRouter() {
console.log(this.$router);
console.log(this.$router.push);
},
//mapMutations 使用方法一
// ...mapMutations([
// 'increase', // 将 `this.increase()` 映射为 `this.$store.commit('increase')`
// ]),
//mapMutations 使用方法二
...mapMutations({
increase: 'increase'// 将 `this.increase()` 映射为 `this.$store.commit('increase')`
})
}
};
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>

vuex mapMutations 使用的更多相关文章

  1. Vue躬行记(9)——Vuex

    Vuex是一个专为Vue.js设计的状态管理库,适用于多组件共享状态的场景.Vuex能集中式的存储和维护所有组件的状态,并提供相关规则保证状态的独立性.正确性和可预测性,这不仅让调试变得可追踪,还让代 ...

  2. vuex 源码分析(七) module和namespaced 详解

    当项目非常大时,如果所有的状态都集中放到一个对象中,store 对象就有可能变得相当臃肿. 为了解决这个问题,Vuex允许我们将 store 分割成模块(module).每个模块拥有自己的 state ...

  3. vuex 源码分析(六) 辅助函数 详解

    对于state.getter.mutation.action来说,如果每次使用的时候都用this.$store.state.this.$store.getter等引用,会比较麻烦,代码也重复和冗余,我 ...

  4. Vue学习日记(四)——Vue状态管理vuex

    前言 先说句前话,如果不是接触大型项目,不需要有多个子页面,不使用vuex也是完全可以的. 说实在话,我在阅读vuex文档的时候,也很难以去理解vuex,甚至觉得没有使用它我也可以.但是直到我在项目碰 ...

  5. vuex辅助函数和vuex5个属性

    在上篇中,我们可以知道如果想要访问vuex.store中state中的数据,需要this.$store.state.属性名.显然这样访问数据写的代码很很不简洁的,辅助函数就是用来解决这个问题的. 1. ...

  6. 逐行粒度的vuex源码分析

    vuex源码分析 了解vuex 什么是vuex vuex是一个为vue进行统一状态管理的状态管理器,主要分为state, getters, mutations, actions几个部分,vue组件基于 ...

  7. vue-router与v-if实现tab切换的思考

    vue-router 该如何使用 忽然碰到一个常见的问题,明明可以使用 v-if / v-show 可以的解决的问题,有没有必要是使用 vue-router来解决. 比如常见的 tab 切换.一时间, ...

  8. 【音乐App】—— Vue-music 项目学习笔记:歌单及排行榜开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 歌单及详情页 排行榜及详情 ...

  9. 08-Vuex

    Vuex 一.简介 ① 是什么:是一个状态管理工具,存放项目组件中的公共数据 二.使用语法 ① 语法 -1. 创建 Vuex 实例 const store = new Vuex.Store({ sta ...

随机推荐

  1. 【Tomcat】一台电脑上运行多个tomcat

    效果: 1.首先需要安装Tomcat7,Tomcat8. Tomcat7: Tomcat8: 2.添加两个环境变量,添加CATALINA_HOME1和CATALINA_BASE1指向E:\tomcat ...

  2. Duplicate the UIButton and Move it

    http://stackoverflow.com/questions/19241208/duplicate-the-uibutton-and-move-it/26438692#26438692 1do ...

  3. Linux有名信号量的创建(sem_open中name参数构造)【转】

    转自:http://blog.csdn.net/gfeng168/article/details/40740865 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.sem_open函数nam ...

  4. python实现websocket

    # websocket实现原理 ''' 1.服务端开启socket,监听ip和端口 2.客户端发送连接请求(带上ip和端口) 3.服务端允许连接 4.客户端生成一个随机字符串,和magic strin ...

  5. linux文件系统之loop环设备--新建一个文件系统并挂载

    1.  /dev目录下有所有已经连接到操作系统上的设备,他们能在/dev里出现就表示他们在硬件层面和系统核心层面被识别了.对于stdin.stdout.zero等设备是可以直接用> <这些 ...

  6. 中控考勤机WEB主动上报接收SERVER程序

    using System; using System.IO; using System.Net; using System.Text.RegularExpressions; namespace Con ...

  7. Ubuntu16.10 +python3.5+Tensorflow 1.1

    1.python版本检查 因为Ubuntu16.10已经默认安装了python2.7 和 3.5,检查python版本, 如果为python2.7,那么就需要我们设置python3.5为默认版本. 查 ...

  8. Python_Tips[4] -> and 和 or 的计算原则

    and和or / and & or 对于and和or,可以连接多个值,其分别遵循原则: 全是 And: 返回第一个遇到的无效值,若全有效,返回最后一个有效值 全是 Or: 返回第一个遇到的有效 ...

  9. bzoj 4338: BJOI2015 糖果

    4338: BJOI2015 糖果 Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 200  Solved: 93[Submit][Status][Dis ...

  10. ostu进行遥感图像的分割

    城市地区道路网的简单的阈值分割.采用的是单ostu(最佳阈值分割)算法,废话少说,如果不太清楚该算法,请参考文献[1]中的图像分割这一章的介绍.程序直接运行的效果如下.