在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 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: {
asyncDecrease({commit }) {
commit(
'decrease',5);
},

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>
<button @click="handleAsyncDecrease">异步-5</button>
<button @click="handleRouter">跳转到 HelloWorld3</button>
<button @click="showRouter">展示路由</button>
</div>
</template> <script>
import { mapState } from 'vuex'
import { mapGetters } from 'vuex'
import { mapMutations } from 'vuex'
import { mapActions } 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);
},
handleAsyncDecrease() {
//调用方式一
// this.$store.dispatch('asyncDecrease');
//调用方式二
this
.asyncDecrease()
},
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')`
}),
//mapActions 使用方法一
// ...mapActions([
// 'asyncDecrease' // 将 `this.asyncDecrease()` 映射为 `this.$store.dispatch('asyncDecrease')`
// ]),
//mapActions 使用方法二
...mapActions({
asyncDecrease: 'asyncDecrease' // 将 `this.asyncDecrease()` 映射为 `this.$store.dispatch('asyncDecrease')`
}),
}
};
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>

vuex mapActions的更多相关文章

  1. vuex Loding加载..

    技术栈:vuex,mapActions, mapState 先在vuex的状态管理里定义好loding状态,以及加载文字 import Vue from 'vue'; import Vuex from ...

  2. Mock(模拟后端接口数据)配合Vuex的使用

    1.下载Mock  cnpm install Mockjs -S 2.新建一个data.js存放新生成的mock文件 编辑mock  并导出 const Mock = require('mockjs' ...

  3. vuex的简单例子和vue model组件

    好久没用过vuex了,vuex官方示例的计算器counter是用的webpack打包了单文件组件,不方便回顾,今天把代码改成了html引人的方式,方便回顾 <!DOCTYPE html> ...

  4. Vuex模块化

    上图是vuex的结构图vuex即 store, 包含State,Action,Mutations, 每一个vue项目都需要使用vuex做组件之间的数据共享 使用场景: 数据最终存放在store的Sta ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. windows实时监测热插拔设备的变化2

    //动态监测设备插拔 #include <Dbt.h> BEGIN_MESSAGE_MAP(ParticipateMeeting, CDialogEx) ON_WM_DEVICECHANG ...

  2. (九)经典_STM32_ADC多通道采样的例子

    STM32 ADC多通道转换描述:用ADC连续采集11路模拟信号,并由DMA传输到内存.ADC配置为扫描并且连续转换模式,ADC的时钟配置为12MHZ.在每次转换结束后,由DMA循环将转换的数据传输到 ...

  3. log4j2 扩展日志级别,支持将系统日志与业务处理日志拆分

    项目中,有时候需要对系统中已处理的一些业务数据日志进行提取分析,通常log4j默认提供的日志级别可能不够用,这时候我们就需要对日志级别进行扩展,以满足我们的需求. 本文就简单介绍一下log4j2的日志 ...

  4. Google C++ 编码规范(中文版)

    Google C++ 编码规范(英文)在线地址:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml 中文版PDF下载:本地下载

  5. AC日记——The Street codechef March challenge 2014

    The Street 思路: 动态开节点线段树: 等差序列求和于取大,是两个独立的子问题: 所以,建两颗线段树分开维护: 求和:等差数列的首项和公差直接相加即可: 取大: 对于线段树每个节点储存一条斜 ...

  6. react setState里的作用域

    从接触racet开始,我们就认识了setState,它是对全局变量进去更新的一个重要方法, 不仅可以更新数据,还能在更新后执行方法时直接调用刚刚更新的数据 今天碰到的问题就在于它的作用域的先后问题 先 ...

  7. elasticsearch REST api

    elasticsearch REST api========================================命令模式:<REST Verb> /<Index>/ ...

  8. [Usaco2010 Feb]Chocolate Buying

    题目描述     贝西和其他奶牛们都喜欢巧克力,所以约翰准备买一些送给她们.奶牛巧克力专卖店里 有N种巧克力,每种巧克力的数量都是无限多的.每头奶牛只喜欢一种巧克力,调查显示, 有Ci头奶牛喜欢第i种 ...

  9. 【字符串】Your Ride Is Here

    题目描述 It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect ...

  10. luogu P1122 最大子树和

    题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...