1、mapState辅助函数
当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键。
(1)首先需要在组件中引用才可以使用 import { mapState } from 'vuex' (2)mapState使用前后对比: // 不使用mapState时:
computed: {
count () {
return this.$store.state.count
}
}
// 使用mapState时:
computed: mapState({
count: state => state.count,
}) 如果在大批量的类似这种的计算属性的话,使用 mapState 会更加便捷。
(3)具体使用
store.js中: import Vue from "vue";
import Vuex from "vuex"; Vue.use(Vuex); const store = new Vuex.Store({
state: {
count: 0,
},
mutations: {
increment(state) {
state.count++;
},
reduction(state) {
state.count--;
}
}
});
export default store; 组件中使用: <template>
<div id="salary-list-second">
<button @click="incrementFun">+</button>
<button @click="reductionFun">-</button>
<p>{{currentCount}}</p>
</div>
</template>
<script>
import { mapState } from "vuex";
export default {
name: "salary-list-second",
computed: mapState({
currentCount: state => state.count
}),
methods: {
incrementFun() {
this.$store.commit("increment");
},
reductionFun() {
this.$store.commit("reduction");
}
}
};
</script>
<style>
button {
padding: 0.2rem;
}
p {
line-height: 0.5rem;
}
</style> (4)mapState和计算属性前后写法的对比举例 // states 为对象时候,mapState转换前
computed: mapState({
count: state => state.count,
// 传字符串参数 'count' 等同于 `state => state.count`
countAlias: 'count',
// 为了能够使用 `this` 获取局部状态,必须使用常规函数
countPlusLocalState (state) {
return state.count + this.localCount
}
}) // states 为对象时候,mapState转换后
computed: {
count() {
return this.$store.state.count
},
countAlias() {
return this.$store.state['count']
},
countPlusLocalState() {
return this.$store.state.count + this.localCount
}
} 使用 Vuex 并不意味着你需要将所有的状态放入 Vuex。虽然将所有的状态放到 Vuex 会使状态变化更显式和易调试,但也会使代码变得冗长和不直观。如果有些状态严格属于单个组件,最好还是作为组件的局部状态。你应该根据你的应用开发需要进行权衡和确定。 2、getter
Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性),就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。
store.js import Vue from "vue";
import Vuex from "vuex"; Vue.use(Vuex); const store = new Vuex.Store({
state: {
storeSalaryList: [
{
name: "马云",
salaryAmount: 1000
},
{
name: "马化腾",
salaryAmount: 900
},
{
name: "李彦宏",
salaryAmount: 800
}
]
},
getters: {
doubleSalaryList(state) {
let newArr = state.storeSalaryList.map(item => {
return {
name: item.name,
salaryAmount: item.salaryAmount * 2 + " " + "$"
};
});
return newArr;
}
});
export default store; 组件中代码: <template>
<div id="salary-list-fisrt">
<h2>财富榜</h2>
<ol>
<li v-for="(salary, index) in getterSalaryList"
:key="index">
{{salary.name}}的工资是:{{salary.salaryAmount}}
</li>
</ol>
</div>
</template>
<script>
export default {
name: "salary-list-first",
computed: {
getterSalaryList() {
return this.$store.getters.doubleSalaryList;
}
}
};
</script> 3、mapGetters辅助函数
store.js中的代码: import Vue from "vue";
import Vuex from "vuex"; Vue.use(Vuex); const store = new Vuex.Store({
state: {
storeSalaryList: [
{
name: "马云",
salaryAmount: 1000
},
{
name: "马化腾",
salaryAmount: 900
},
{
name: "李彦宏",
salaryAmount: 800
}
]
},
getters: {
doubleSalaryList(state) {
let newArr = state.storeSalaryList.map(item => {
return {
name: item.name,
salaryAmount: item.salaryAmount * 2 + " " + "$"
};
});
return newArr;
},
totalSalary(state) {
let sum = 0;
state.storeSalaryList.map(item => {
sum += item.salaryAmount;
});
return sum * 2;
}
}
});
export default store; 组件中的应用: <template>
<div id="salary-list-fisrt">
<h2>财富榜</h2>
<ol>
<li v-for="(salary, index) in myDoubleSalaryGetter"
:key="index">
{{salary.name}}的工资是:{{salary.salaryAmount}}
</li>
</ol>
<span>工资总额是:{{myTotalSalary}} $</span>
</div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
name: "salary-list-first",
computed: {
...mapGetters({
myDoubleSalaryGetter: "doubleSalaryList",
myTotalSalary: "totalSalary"
})
}
};
</script>

vuex学习与实践——mapState、getter、mapGetters的更多相关文章

  1. Vue学习之--------深入理解Vuex之getters、mapState、mapGetters(2022/9/3)

    这一篇博客的内容是在上一篇博客的基础上进行:深入理解Vuex.原理详解.实战应用 @ 目录 1.getters的使用 1.1 概念 1.2 用法 1.3 如何读取数据 2.getters在项目中的实际 ...

  2. Vuex实践(下)-mapState和mapGetters

    Vuex系列文章 <Vuex实践(上)> <Vuex实践(中)-多module中的state.mutations.actions和getters> <Vuex实践(下)- ...

  3. vue:vuex中mapState、mapGetters、mapActions辅助函数及Module的使用

    一.普通store中使用mapState.mapGetters辅助函数: 在src目录下建立store文件夹: ​ index.js如下: import Vue from 'vue'; import ...

  4. 挑战全网最幽默的Vuex系列教程:第二讲 Vuex旗下的State和Getter

    先说两句 上一讲 「Vuex 到底是个什么鬼」,已经完美诠释了 Vuex 的牛逼技能之所在(纯属自嗨).如果把 Vuex 比喻成农药里面的刘备,那就相当于你现在已经知道了刘备他是一个会打枪的力量型英雄 ...

  5. vuex学习总结

    vuex 学习 mapState,mapGetters 一般也写在 computed 中 , mapActions 一般写在 methods中.

  6. Vue学习—— Vuex学习笔记

    组件是Vue最强大的功能之一,而组件实例的作用域是相互独立的,意味着不同组件之间的数据是无法相互使用.组件间如何传递数据就显得至关重要,这篇文章主要是介绍Vuex.尽量以通俗易懂的实例讲述这其中的差别 ...

  7. hadoop2.5.2学习及实践笔记(二)—— 编译源代码及导入源码至eclipse

    生产环境中hadoop一般会选择64位版本,官方下载的hadoop安装包中的native库是32位的,因此运行64位版本时,需要自己编译64位的native库,并替换掉自带native库. 源码包下的 ...

  8. ansible 学习与实践

    title: ansible 学习与实践 date: 2016-05-06 16:17:28 tags: --- ansible 学习与实践 一 介绍 ansible是新出现的运维工具是基于Pytho ...

  9. Google App Engine 学习和实践

    这个周末玩了玩Google App Engine,随手写点东西,算是学习笔记吧.不当之处,请多多指正. 作者:liigo,2009/04/26夜,大连 原创链接:http://blog.csdn.ne ...

随机推荐

  1. 电影编码JPEG2000与H.264

    电影的第三次革命是数字电影的诞生,数字电影取代了胶片,那么数字电影就一定有其独特的封装(压缩)格式.在网络上,我们经常见到许多视频格式,诸如mp4.mkv.flv.rmvb等,这些都是在通用计算机上播 ...

  2. python之块包导入

    一.模块 1.什么是模块 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...

  3. 【题解】Luogu P2073 送花

    原题传送门 这题需要用到Splay 我们用一棵splay维护金钱 考虑c<=1000000 我们珂以把每种价格现在对应的美丽值存在一个a数组中 这样讲有珂能不太清楚qaq,还是对着操作一个一个讲 ...

  4. Java基础语法(下)

    1.数组动态初始化 //数据类型[] 数组名 = new 数据类型[数组长度]; int[] arr = new int[3]; /* * 左边: * int:说明数组中的元素类型是int类型 * [ ...

  5. STL相关问题

    写set容器遇到以下问题: C:\Users\admin\Desktop\未命名2.cpp In function 'int main()': 67 98 C:\Users\admin\Desktop ...

  6. Docker基础学习-尚硅谷

    视频地址:链接: https://pan.baidu.com/s/15sJuEh5cVTJ7-vWSH7vffw 提取码: zf25 笔记:

  7. EMMC 介绍【转】

    本文转载自:https://blog.csdn.net/u014645605/article/details/52061034 定义: eMMC (Embedded Multi Media Card) ...

  8. linux下如何删除行首的数字?

    举例如下: 1.某文件jello.txt中有以下行 1111-yes 2222-no 3333-yes-no-no 2.删除jello.txt中每行行首的数字 cat jello.txt | cut ...

  9. Linux command: grep

    How to use grep to match multiple strings in the same line? grep 'string1\|string2' filename grep -E ...

  10. 题解——洛谷P1250 种树(差分约束)

    一道看一眼就知道差分约束的题目 但是最短路spfa的时候注意松弛条件是 if(dis[u]+w[i]<dis[v[i]]) dis[v[i]]=dis[u]+w[i]; 不能写成 if(dis[ ...