使用 @ngneat/until-destroy 管理 Angular 组件中的 RxJS 订阅
项目介绍
@ngneat/until-destroy
是一个用于 Angular 的开源库,旨在简化在组件销毁时自动取消 RxJS 订阅的管理。通过使用 @UntilDestroy
装饰器和 untilDestroyed
操作符,开发者可以轻松地确保在组件销毁时自动取消订阅,从而避免内存泄漏和潜在的性能问题。
项目快速启动
安装
首先,你需要在你的 Angular 项目中安装 @ngneat/until-destroy
:
npm install @ngneat/until-destroy
或者使用 Yarn:
yarn add @ngneat/until-destroy
使用
在你的 Angular 组件中使用 @UntilDestroy
装饰器和 untilDestroyed
操作符来管理订阅。
import { Component, OnInit } from '@angular/core';
import { interval } from 'rxjs';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
@UntilDestroy()
@Component({
selector: 'app-inbox',
templateUrl: './inbox.component.html',
styleUrls: ['./inbox.component.css']
})
export class InboxComponent implements OnInit {
ngOnInit() {
interval(1000)
.pipe(untilDestroyed(this))
.subscribe(val => console.log(val));
}
}
在这个例子中,interval(1000)
每秒会发出一个值,并通过 untilDestroyed(this)
操作符确保在组件销毁时自动取消订阅。
应用案例和最佳实践
自动取消订阅属性
你可以通过设置 checkProperties
选项来自动取消订阅组件中的订阅属性:
@UntilDestroy({ checkProperties: true })
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
subscription = fromEvent(document, 'mousemove').subscribe();
}
取消指定数组中的订阅
如果你有一个订阅数组,可以通过设置 arrayName
选项来取消数组中每个订阅:
@UntilDestroy({ arrayName: 'subscriptions' })
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
subscriptions = [
fromEvent(document, 'click').subscribe(),
fromEvent(document, 'mousemove').subscribe()
];
}
黑名单
如果你不想取消某些特定的订阅,可以通过设置 blackList
选项来排除它们:
@UntilDestroy({ checkProperties: true, blackList: ['subscription1'] })
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
subscription1: Subscription;
subscription2: Subscription;
constructor() {
this.subscription1 = new Subject().subscribe();
this.subscription2 = new Subject().subscribe();
}
}
在这个例子中,subscription1
不会在组件销毁时被取消订阅,而 subscription2
会自动取消订阅。
典型生态项目
Angular
@ngneat/until-destroy
是 Angular 生态系统中的一个重要工具,特别适用于需要管理大量 RxJS 订阅的复杂应用。它可以帮助开发者避免常见的内存泄漏问题,并提高应用的性能。
RxJS
@ngneat/until-destroy
与 RxJS 紧密集成,提供了简洁的 API 来管理 RxJS 订阅。它适用于任何使用 RxJS 的 Angular 项目,尤其是那些需要处理大量异步数据流的应用。
Angular CLI
Angular CLI 是 Angular 项目的标准构建工具,@ngneat/until-destroy
可以无缝集成到 Angular CLI 项目中,提供自动化的订阅管理功能。
通过使用 @ngneat/until-destroy
,你可以大大简化 Angular 应用中的订阅管理,确保应用的稳定性和性能。
PaddleOCR-VL
PaddleOCR-VL 是一款顶尖且资源高效的文档解析专用模型。其核心组件为 PaddleOCR-VL-0.9B,这是一款精简却功能强大的视觉语言模型(VLM)。该模型融合了 NaViT 风格的动态分辨率视觉编码器与 ERNIE-4.5-0.3B 语言模型,可实现精准的元素识别。Python00- DDeepSeek-V3.2-ExpDeepSeek-V3.2-Exp是DeepSeek推出的实验性模型,基于V3.1-Terminus架构,创新引入DeepSeek Sparse Attention稀疏注意力机制,在保持模型输出质量的同时,大幅提升长文本场景下的训练与推理效率。该模型在MMLU-Pro、GPQA-Diamond等多领域公开基准测试中表现与V3.1-Terminus相当,支持HuggingFace、SGLang、vLLM等多种本地运行方式,开源内核设计便于研究,采用MIT许可证。【此简介由AI生成】Python00
openPangu-Ultra-MoE-718B-V1.1
昇腾原生的开源盘古 Ultra-MoE-718B-V1.1 语言模型Python00ops-transformer
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。C++0123AI内容魔方
AI内容专区,汇集全球AI开源项目,集结模块、可组合的内容,致力于分享、交流。02Spark-Chemistry-X1-13B
科大讯飞星火化学-X1-13B (iFLYTEK Spark Chemistry-X1-13B) 是一款专为化学领域优化的大语言模型。它由星火-X1 (Spark-X1) 基础模型微调而来,在化学知识问答、分子性质预测、化学名称转换和科学推理方面展现出强大的能力,同时保持了强大的通用语言理解与生成能力。Python00GOT-OCR-2.0-hf
阶跃星辰StepFun推出的GOT-OCR-2.0-hf是一款强大的多语言OCR开源模型,支持从普通文档到复杂场景的文字识别。它能精准处理表格、图表、数学公式、几何图形甚至乐谱等特殊内容,输出结果可通过第三方工具渲染成多种格式。模型支持1024×1024高分辨率输入,具备多页批量处理、动态分块识别和交互式区域选择等创新功能,用户可通过坐标或颜色指定识别区域。基于Apache 2.0协议开源,提供Hugging Face演示和完整代码,适用于学术研究到工业应用的广泛场景,为OCR领域带来突破性解决方案。00- HHowToCook程序员在家做饭方法指南。Programmer's guide about how to cook at home (Chinese only).Dockerfile011
- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00
项目优选









