说说Vuex的getters属性的具体用法
网络编程 2021-07-04 15:50www.168986.cn编程入门
这篇文章主要介绍了说说Vuex的getters属性的具体用法,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
什么是getters
在介绍state中我们了解到,在Store仓库里,state就是用来存放数据,若是对数据进行处理输出,比如数据要过滤,一般我们可以写到puted中。但是如果很多组件都使用这个过滤后的数据,比如饼状图组件和曲线图组件,我们是否可以把这个数据抽提出来共享?这就是getters存在的意义。我们可以认为,【getters】是store的计算属性。
源码分析
wrapGetters初始化getters,接受3个参数,store表示当前的Store实例,moduleGetters当前模块下所有的getters,modulePath对应模块的路径
function `wrapGetters` (store, moduleGetters, modulePath) { Object.keys(moduleGetters).forEach(getterKey => { // 遍历先所有的getters const rawGetter = moduleGetters[getterKey] if (store._wrappedGetters[getterKey]) { console.error(`[vuex] duplicate getter key: ${getterKey}`) // getter的key不允许重复,否则会报错 return } store._wrappedGetters[getterKey] = function `wrappedGetter` (store{ // 将每一个getter包装成一个方法,并且添加到store._wrappedGetters对象中, return rawGetter( //执行getter的回调函数,传入三个参数,(local state,store getters,rootState) getNestedState(store.state, modulePath), // local state //根据path查找state上嵌套的state store.getters, // store上所有的getters store.state // root state)}}) } //根据path查找state上嵌套的state function `getNestedState` (state, path) { return path.length ? path.reduce((state, key) => state[key], state): state}
1 应用场景
假设我们在 Vuex 中定义了一个数组:
const store = new Vuex.Store({ state: { list:[1,3,5,7,9,20,30] } ... })
业务场景希望过滤出大于 5 的数。马上想到的方法可能的是:在组件的计算属性中进行过滤:
<template> <div> {{list}} </div> </template> <script> export default { name: "index.vue", puted: { list() { return this.$store.state.list.filter(item => item > 5); } } } </script>
效果:
功能虽然实现了,但如果其它组件也需要过滤后的数据,那么就得把 index.vue 中的计算过滤代码复制出来。如果过滤规则发生变化,还得一一修改这些组件中的计算属性,很难维护。这种场景下,我们就可以使用 getters 属性啦O(∩_∩)O~
2 基础用法
main.js:
const store = new Vuex.Store({ state: { list: [1, 3, 5, 7, 9, 20, 30] }, getters: { filteredList: state => { return state.list.filter(item => item > 5) } } })
index.vue:
<script> export default { name: "index.vue", puted: { list() { return this.$store.getters.filteredList; } } } </script>
效果达到了,而且只需要在一处维护过滤规则即可。
3 内部依赖
getter 可以依赖其它已经定义好的 getter。比如我们需要统计过滤后的列表数量,就可以依赖之前定义好的过滤函数。
main.js:
const store = new Vuex.Store({ state: { list: [1, 3, 5, 7, 9, 20, 30] }, getters: { filteredList: state => { return state.list.filter(item => item > 5) }, listCount: (state, getters) => { return getters.filteredList.length; } } })
index.vue:
<template> <div> 过滤后的列表:{{list}} <br> 列表长度:{{listCount}} </div> </template> <script> export default { name: "index.vue", puted: { list() { return this.$store.getters.filteredList; }, listCount() { return this.$store.getters.listCount; } } } </script>
效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程