在介绍state中我们了解到,在Store仓库里,state就是用来存放数据,若是对数据进行处理输出,比如数据要
过滤,一般我们可以写到computed中。但是如果很多组件都使用这个过滤后的数据,比如饼状图组件和曲线图
组件,我们是否可以把这个数据抽提出来共享?这就是getters存在的意义。我们可以认为,【getters】是
store的计算属性。2.如何使用定义:我们可以在store中定义getters,第一个参数是state
?1const getters = {style:state => state.style}传参:定义的Getters会暴露为store.getters对象,
也可以接受其他的getters作为第二个参数;
使用:
computed: {
doneTodosCount () {
return this.$store.getters.doneTodosCount
}
3.mapGetters
mapGetters辅助函数仅仅是将store中的getters映射到局部计算属性中,用法和mapState类似
?1234567891011import { mapGetters } from 'vuex'computed: { // 使用对象展开运算符将 getters
混入 computed 对象中 ...mapGetters([ 'doneTodosCount', 'anotherGetter',])} //给getter属性
换名字 mapGetters({ // 映射 this.doneCount 为 store.getters.doneTodosCount
doneCount: 'doneTodosCount'})复制代码
转载于:https://juejin.im/post/5a4922895188257d167a8ab3