1.什么是getters
在介绍中我们了解到,在Store
仓库里,state
就是用来存放数据,若是对数据进行处理输出,比如数据要过滤,一般我们可以写到computed
中。但是如果很多组件都使用这个过滤后的数据,比如饼状图组件和曲线图组件,我们是否可以把这个数据抽提出来共享?这就是getters
存在的意义。我们可以认为,【getters】是store的计算属性。
2.如何使用
定义:我们可以在store
中定义getters
,第一个参数是state
传参:定义的Getters
会暴露为store.getters
对象,也可以接受其他的getters
作为第二个参数;
使用:
3.mapGetters
mapGetters
辅助函数仅仅是将store
中的getters
映射到局部计算属性中,用法和mapState
类似
4.源码分析
wrapGetters
初始化getters
,接受3个参数,store
表示当前的Store
实例,moduleGetters
当前模块下所有的getters
,modulePath
对应模块的路径
//根据path查找state上嵌套的state
function get<a href="https://www.jb51.cc/tag/nes/" target="_blank" class="keywords">nes</a>tedState
(state,path) {
return path.length
? path.reduce((state,key) => state[key],state): state}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。
原文地址:https://www.jb51.cc/vue/39868.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。