使用 @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 应用中的订阅管理,确保应用的稳定性和性能。
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00
GLM-4.7-FlashGLM-4.7-Flash 是一款 30B-A3B MoE 模型。作为 30B 级别中的佼佼者,GLM-4.7-Flash 为追求性能与效率平衡的轻量化部署提供了全新选择。Jinja00
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin07
compass-metrics-modelMetrics model project for the OSS CompassPython00