ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

ng-zorro-antd 的 nzAggregate 管道:一行代码完成数组 Sum、Max、Min、Average 聚合计算

ng-zorro-antd 的 nzAggregate 管道:一行代码完成数组 Sum、Max、Min、Average 聚合计算 UI组件前端【免费下载链接】ng-zorro-antdAngular UI Component Library based on Ant Design项目地址https://gitcode.com/gh_mirrors/ng/ng-zorro-antd点击查看免费下载本文介绍 NG-ZORROng-zorro-antd通用 Pipes 集合中的nzAggregate管道一个专门用于对数值数组执行求和sum、最大值max、最小值min、平均值avg等聚合操作的 Angular 管道。它适用于仪表盘统计卡片、数据报表摘要、图表示例数据等场景读完后你将掌握该管道的引入方式、完整参数用法、模板写法与底层实现原理并能结合源码与测试用例理解其边界行为。nzAggregate 是什么数组聚合操作的声明式方案在 Angular 模板中处理数组统计需求时传统做法是先在组件类中编写reduce、Math.max等逻辑再绑定到模板代码分散且难以复用。nzAggregate管道把这类高频聚合操作封装为纯函数式管道让你在模板中直接声明式计算{{ [7, 8, 2, 3] | nzAggregate: sum }}该管道是 ng-zorro-antd 的 Pipes 组件集合位于 components/pipes中六个内置管道之一与其同级的是nzBytes、nzToCssUnit、nzEllipsis、nzSanitizer、nzTrim。其官方功能说明见 demo/aggregate.md为数组的 Sum、Min、Max、Average 等聚合操作Sum, Min, Max, Average and other operations on arrays。引入与注册方式nzAggregate由NzAggregatePipe实现需要通过NzPipesModule或 Angular 现代组件式导入两种方式使用方式一模块注册NgModule在NzPipesModule定义见 nz-pipes.module.ts中NzAggregatePipe与其他管道一同被声明并导出因此只需在特性模块中引入该模块即可在模板中使用全部管道import { NgModule } from angular/core; import { NzPipesModule } from ng-zorro-antd/pipes; NgModule({ imports: [NzPipesModule] }) export class MyModule {}方式二组件级导入Standalone从源码结构看仓库的演示组件采用 Standalone 方式按需导入单个管道import { Component } from angular/core; import { NzAggregatePipe } from ng-zorro-antd/pipes; Component({ selector: my-aggregate-demo, imports: [NzAggregatePipe], template: ... }) export class MyAggregateDemoComponent {}ng-zorro-antd/pipes的公开导出入口见 public-api.ts其中导出了nz-aggregate.pipe与nz-pipes.module两种方式均可靠。API 与参数说明官方 API 文档doc/index.zh-CN.md对nzAggregate的参数定义如下参数说明类型默认值method聚合方式sum \| max \| min \| avg结合 nz-aggregate.pipe.ts 的实现四种聚合方式的语义为method 取值计算逻辑返回值示例输入[7, 8, 2, 3]sum数组全部元素累加和20max数组最大值8min数组最小值2avg数组平均值和 ÷ 长度5类型定义同样来自源码export type AggregateMethod sum | max | min | avg。传入管道的不再是普通字符串而是类型安全的联合类型能获得 IDE 的自动补全与编译期校验。模板中的典型用法结合 nz-statistic 的统计卡片仓库官方演示demo/aggregate.ts给出了一套可直接复用的完整模板使用nz-row/nz-col栅格布局配合nz-statistic统计组件将四种聚合结果并列展示nz-row [nzGutter]16 nz-col [nzSpan]6 nz-statistic [nzValue][7, 8, 2, 3] | nzAggregate: max nzTitleMax [7, 8, 2, 3] / /nz-col nz-col [nzSpan]6 nz-statistic [nzValue][7, 8, 2, 3] | nzAggregate: min nzTitleMin [7, 8, 2, 3] / /nz-col nz-col [nzSpan]6 nz-statistic [nzValue][7, 8, 2, 3] | nzAggregate: sum nzTitleSum [7, 8, 2, 3] / /nz-col nz-col [nzSpan]6 nz-statistic [nzValue][7, 8, 2, 3] | nzAggregate: avg nzTitleAvg [7, 8, 2, 3] / /nz-col /nz-row演示组件通过imports: [NzGridModule, NzStatisticModule, NzAggregatePipe]显式引入所需依赖。注意[7, 8, 2, 3] | nzAggregate: max的写法管道参数method放在冒号之后。由于该管道为纯管道Angular 会在输入数组引用未变化时复用上一次的求值结果模板表达式保持简洁且无额外性能开销。底层实现原理与边界行为核心实现NzAggregatePipe实现PipeTransform接口核心逻辑nz-aggregate.pipe.ts如下transform(value: number[], method: AggregateMethod): undefined | number { if (!Array.isArray(value)) { return value; } if (value.length 0) { return undefined; } switch (method) { case sum: return sum(value); case avg: return sum(value) / value.length; case max: return Math.max(...value); case min: return Math.min(...value); default: throw Error(Invalid Pipe Arguments: Aggregate pipe doesnt support this type); } }关键实现细节求和复用核心工具函数sum与avg分支都调用了ng-zorro-antd/core/util导出的sum工具函数其定义见 components/core/util/number.tsexport function sum(input: number[], initial: number 0): number { return input.reduce((previous: number, current: number) previous current, initial); }它基于数组reduce实现并允许传入initial初始值默认0。avg即sum(value) / value.length。极值与展开运算max/min借助 ES6 展开语法将数组展开为Math.max(...value)/Math.min(...value)等价于Math.max(7, 8, 2, 3)。从源码结构看对于超大规模数组展开语法存在栈溢出风险日常报表场景数据量通常在数百以内不受影响。空数组返回undefined聚合结果无法定义时返回undefined恰好可与nz-statistic等组件的空值语义衔接。非法 method 抛出异常switch的default分支抛出Invalid Pipe Arguments: Aggregate pipe doesnt support this type提示开发者传入的聚合方式不受支持——这是类型联合仍可能被绕过如as any或来自外部数据时的运行时兜底校验。非数组输入原样透传!Array.isArray(value)时直接返回value避免对null、undefined或非数组值执行聚合时报错。测试用例验证仓库配套单元测试nz-aggregate.pipe.spec.ts覆盖了全部四种方法可作为行为规范的权威参考max[1, 2, 3, 4]→4[1]→1[1, 1]→1[]→undefinedmin[1, 2, 3, 4]→1[4, 3, 2, 5]→2[]→undefinedsum[1, 2, 3, 4]→10[1]→1[1, 1]→2avg[1, 2, 3, 4]→2.5[1]→1[1, 1]→1[]→undefined单元素数组在所有方法下均返回该元素本身空数组统一返回undefined这些断言与源码逻辑一一对应。最佳实践小结优先在模板中直接用| nzAggregate: sum完成展示型聚合无需在组件类里手写reduce与循环聚合数据来自响应式状态时注意保持数组引用稳定如使用不可变更新以发挥纯管道的缓存收益需要自定义初始值、小数位控制或更多聚合算法如方差、中位数时可参考sum工具函数components/core/util/number.ts自行扩展该文件还提供了toDecimal、getPrecision、ensureNumberInRange等数值工具可组合使用遇到非法method报错Invalid Pipe Arguments时检查传入的聚合方式是否在sum | max | min | avg四种取值之内。nzAggregate以约 20 行实现覆盖了报表与统计场景最常用的四种数组聚合运算配合NzPipesModule的集中导出是 ng-zorro-antd 生态中小而实用的工具型管道代表。赞分享UI组件前端【免费下载链接】ng-zorro-antdAngular UI Component Library based on Ant Design项目地址https://gitcode.com/gh_mirrors/ng/ng-zorro-antd点击查看免费下载相关推荐MateCloud API网关JWT认证与权限验证流程完全指南 MateCloud API网关JWT认证与权限验证流程完全指南 MateCloud作为一款基于Spring Cloud Alibaba的微服务架构平台后端API网关微服务认证鉴权AI 应用前端云原生ng-zorro-antd Pipes 管道集合实战指南nzBytes / nzToCssUnit / nzEllipsis / nzAggregate / nzSanitizer 用法与源码解析ng zorro antd Pipes 管道集合实战指南nzBytes / nzToCssUnit / nzEllipsis / nzAggregate /UI组件前端如何使用APIJSON实现高效聚合查询sum、count、max、min、avg函数全攻略如何使用APIJSON实现高效聚合查询sum、count、max、min、avg函数全攻略 APIJSON是一个基于JSON风格的API开发框架支持MySQ后端ORM低代码创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表