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. 学写网页 #06# table

    A B E C D <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...

  2. Selenium在Firefox中踩过的

    本文转至 http://www.51testing.com/html/11/n-3711311.html,作者对webdriver在Firefox中设置profile配置项挺熟的,是用Python实现 ...

  3. PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转

    ChineseUtil 下载地址:https://github.com/Yurunsoft/ChineseUtil 另外一个中文转拼音工具:https://github.com/overtrue/pi ...

  4. git获取一个版本相对于另一个版本新增,修改,删除的文件

    git diff --name-status 00ef237ef0f0a4b8bd9609c2b6d570472028212d abf13b4d58abbb05a7d494cdc205d025978a ...

  5. spring boot mvc系列-静态资源配置与MappingHandler拦截器

    静态资源配置 Spring Boot 默认将 /** 所有访问映射到以下目录: classpath:/static classpath:/public classpath:/resources cla ...

  6. NLTK 知识整理

    NLTK 知识整理 nltk.corpus模块自带语料 NLTK comes with many corpora, toy grammars, trained models, etc. A compl ...

  7. python简说(二十二)写日志

    分四个级别 import nnloglog = nnlog.Logger('book_server.log') log.debug('xxx值是什么')log.info('调用了什么xxx')log. ...

  8. PowerShell Gallery

    https://docs.microsoft.com/en-us/powershell/gallery/getting-started https://www.powershellgallery.co ...

  9. hihoCoder week15 最近公共祖先·二

    tarjan求lca  就是dfs序中用并查集维护下,当访问到询问的第二个点u的时候  lca就是第一点的find(fa[v]) fa[v] = u; // 当v为u的儿子 且 v已经dfs完毕 #i ...

  10. 到底什么是 ROI Pooling Layer ???

    到底什么是 ROI Pooling Layer ??? 只知道 faster rcnn 中有 ROI pooling, 而且其他很多算法也都有用这个layer 来做一些事情,如:SINT,检测的文章等 ...