vuex mapActions
在组件中使用 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的更多相关文章
- vuex Loding加载..
技术栈:vuex,mapActions, mapState 先在vuex的状态管理里定义好loding状态,以及加载文字 import Vue from 'vue'; import Vuex from ...
- Mock(模拟后端接口数据)配合Vuex的使用
1.下载Mock cnpm install Mockjs -S 2.新建一个data.js存放新生成的mock文件 编辑mock 并导出 const Mock = require('mockjs' ...
- vuex的简单例子和vue model组件
好久没用过vuex了,vuex官方示例的计算器counter是用的webpack打包了单文件组件,不方便回顾,今天把代码改成了html引人的方式,方便回顾 <!DOCTYPE html> ...
- Vuex模块化
上图是vuex的结构图vuex即 store, 包含State,Action,Mutations, 每一个vue项目都需要使用vuex做组件之间的数据共享 使用场景: 数据最终存放在store的Sta ...
- Vue躬行记(9)——Vuex
Vuex是一个专为Vue.js设计的状态管理库,适用于多组件共享状态的场景.Vuex能集中式的存储和维护所有组件的状态,并提供相关规则保证状态的独立性.正确性和可预测性,这不仅让调试变得可追踪,还让代 ...
- vuex 源码分析(七) module和namespaced 详解
当项目非常大时,如果所有的状态都集中放到一个对象中,store 对象就有可能变得相当臃肿. 为了解决这个问题,Vuex允许我们将 store 分割成模块(module).每个模块拥有自己的 state ...
- vuex 源码分析(六) 辅助函数 详解
对于state.getter.mutation.action来说,如果每次使用的时候都用this.$store.state.this.$store.getter等引用,会比较麻烦,代码也重复和冗余,我 ...
- vuex辅助函数和vuex5个属性
在上篇中,我们可以知道如果想要访问vuex.store中state中的数据,需要this.$store.state.属性名.显然这样访问数据写的代码很很不简洁的,辅助函数就是用来解决这个问题的. 1. ...
- 逐行粒度的vuex源码分析
vuex源码分析 了解vuex 什么是vuex vuex是一个为vue进行统一状态管理的状态管理器,主要分为state, getters, mutations, actions几个部分,vue组件基于 ...
随机推荐
- MFC 菜单操作
在CMainFrame中OnCreate函数中添加下列:(注意在return(0)前添加) 在文件,新建前打钩 法1: GetMenu()->GetSubMenu(0)->CheckMen ...
- screenshoter 連續截圖工具
https://pcrookie.com/?p=993 顯示 mouse 設定 Settings -> Saving -> Display mouse cursor
- 【C语言】32位,64位机器sizeof区别
float, double : 采用IEEE标准浮点数格式,格式固定 float 32bit, double 64bit int一般和CPU寄存器长度有关,不过也和编译器,汇编器有关 由于C ,C++ ...
- C#集合类:动态数组、队列、栈、哈希表、字典(转)
1.动态数组:ArrayList 主要方法:Add.AddRange.RemoveAt.Remove 2.队列:Queue 主要方法:Enqueue入队列.Dequeue出队列.Peek返回Queue ...
- linux文件名匹配
* 匹配文件名中的任何字符串,包括空字符串. ? 匹配文件名中的任何单个字符. [...] 匹配[ ]中所包含的任何字符. [!...] 匹配[ ]中非感叹号!之后的字符. 如: s* ...
- 洛谷 P1064 金明的预算方案【DP/01背包-方案数】
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:"随便点". 题目描述 不过ui ...
- 洛谷 P1181 数列分段Section I【贪心/最少分成多少合法的连续若干段】
题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成连续的若干段,并且每段和不超过M(可以等于M),问最少能将其分成多少段使得满足要求. 输入输出格式 输入格式: 输入文件divide_ ...
- Python之端口扫描器编写
其实,写个扫描器也挺好玩的,牵涉到了RAW Socket编程,可以尽情地DIY数据包(当然,不符合数据包规则,比如checksum错误就没办法了),收获颇深.其中,我觉得用C语言写更有利于在编写过程中 ...
- x-pack-crack
破解x-pack-----------1. 编辑文件:LicenseVerifier.javapackage org.elasticsearch.license;import java.nio.*;i ...
- Best Time to Buy and Sell Stock with Cooldown -- LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...