angular - 我需要取消订阅 Angular Observable 吗?

关于是否以及何时您应该取消订阅 Angular Observable 的问题,很难得到一个直接的答案。

我有以下场景:

this.subscription = this.service.makeHTTPCall(stuff).subscribe(x => {
//do something
});

我看到了一些解决方案:

  1. 不要将订阅存储为变量,这是否意味着我不必取消订阅?

    this.service.makeHTTPCall(stuff).subscribe(x => {
    //do something
    });
    
  2. 将订阅存储为变量并在 ngOnDestroy 中取消订阅

    ngOnDestroy() {
    if (this.subscription) { this.subscription.unsubscribe(); }
    }
    
  3. 什么都不做,Angular 会为您整理所有取消订阅的内容

我知道有像 ng-take-until-destroy 这样的第三方库,但假设我们没有任何第三方库,这是取消订阅的建议方法吗?

相关文章:

c++ - 多线程段故障析构函数

python - Matplotlib Colormaps——为每个图形/线条/主题选择不同的颜色

php - arc diff(Arcanist)因 env : php: No such file

c - 在两个源文件之间拆分类似函数的宏调用

angular - "declare global{ interface Window{ analy

clojure - 撤消覆盖 Clojure 中的内置函数

python - Ursina Python 引擎 : Lighting, 阴影和光晕效果

linux - NeoVim 缺少 Python 支持

typescript - 如何防止将 Prop 传递给内部样式组件

encryption - 如何在 Web 浏览器中使用 Webauthn/CTAP HMAC-Sec