使用 @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 应用中的订阅管理,确保应用的稳定性和性能。
ERNIE-4.5-VL-28B-A3B-ThinkingERNIE-4.5-VL-28B-A3B-Thinking 是 ERNIE-4.5-VL-28B-A3B 架构的重大升级,通过中期大规模视觉-语言推理数据训练,显著提升了模型的表征能力和模态对齐,实现了多模态推理能力的突破性飞跃Python00
unified-cache-managementUnified Cache Manager(推理记忆数据管理器),是一款以KV Cache为中心的推理加速套件,其融合了多类型缓存加速算法工具,分级管理并持久化推理过程中产生的KV Cache记忆数据,扩大推理上下文窗口,以实现高吞吐、低时延的推理体验,降低每Token推理成本。Python03
Kimi-K2-ThinkingKimi K2 Thinking 是最新、性能最强的开源思维模型。从 Kimi K2 开始,我们将其打造为能够逐步推理并动态调用工具的思维智能体。通过显著提升多步推理深度,并在 200–300 次连续调用中保持稳定的工具使用能力,它在 Humanity's Last Exam (HLE)、BrowseComp 等基准测试中树立了新的技术标杆。同时,K2 Thinking 是原生 INT4 量化模型,具备 256k 上下文窗口,实现了推理延迟和 GPU 内存占用的无损降低。Python00
Spark-Prover-X1-7BSpark-Prover-X1-7B is a 7B-parameter large language model developed by iFLYTEK for automated theorem proving in Lean4. It generates complete formal proofs for mathematical theorems using a three-stage training framework combining pre-training, supervised fine-tuning, and reinforcement learning. The model achieves strong formal reasoning performance and state-of-the-art results across multiple theorem-proving benchmarksPython00
MiniCPM-V-4_5MiniCPM-V 4.5 是 MiniCPM-V 系列中最新且功能最强的模型。该模型基于 Qwen3-8B 和 SigLIP2-400M 构建,总参数量为 80 亿。与之前的 MiniCPM-V 和 MiniCPM-o 模型相比,它在性能上有显著提升,并引入了新的实用功能Python00
Spark-Formalizer-X1-7BSpark-Formalizer-X1-7B is a 7B-parameter large language model by iFLYTEK for mathematical auto-formalization. It translates natural-language math problems into precise Lean4 formal statements, achieving high accuracy and logical consistency. The model is trained with a two-stage strategy combining large-scale pre-training and supervised fine-tuning for robust formal reasoning.Python00
GOT-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).Dockerfile015
Spark-Scilit-X1-13B科大讯飞Spark Scilit-X1-13B基于最新一代科大讯飞基础模型,并针对源自科学文献的多项核心任务进行了训练。作为一款专为学术研究场景打造的大型语言模型,它在论文辅助阅读、学术翻译、英语润色和评论生成等方面均表现出色,旨在为研究人员、教师和学生提供高效、精准的智能辅助。Python00- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00