主题'完整' rxjs的完整订阅并未取消订阅 - 角

发布于 2025-01-31 21:00:30 字数 875 浏览 3 评论 0原文

RXJ的主题“完整”并未在Angular中取消订阅。

我正在使用“ takeuntil”未订阅方法。 我的应用程序中有多个订阅,我想在“ ngondestroy”中退订所有这些订阅。

我创建了以下逻辑:

@Component({
  selector: "app-example",
  templateUrl: "./example.component.html"
})
export class Example extends onDestroy { 

  private unsubscribe: Subject<void> = new Subject();

  // ... some subscriptions: 
  // observable.pipe(takeUntil(this.unsubscribe)).subscribe(/* do something */)

  ngOnDestroy() {
     this.unsubscribe.next();
     this.unsubscribe.complete();    
     
      if (this.unsubscribe.closed) {
        //subscribed
        console.log("unsubscribed");
      } else console.log("subscribed");
  }
}

并将其登录到“订阅”控制台。

我在所有管道中使用的唯一操作员是“ takeuntil”。

我应该使用'.unsubscribe'而不是“ .complete”?

Subject 'complete' of rxjs is not unsubscribing on complete in Angular.

I am using the 'takeUntil' unsubscription approach.
I have multiple subscriptions in my app and I would like to unsubscribe all of them in 'ngOnDestroy'.

I have created the following logic:

@Component({
  selector: "app-example",
  templateUrl: "./example.component.html"
})
export class Example extends onDestroy { 

  private unsubscribe: Subject<void> = new Subject();

  // ... some subscriptions: 
  // observable.pipe(takeUntil(this.unsubscribe)).subscribe(/* do something */)

  ngOnDestroy() {
     this.unsubscribe.next();
     this.unsubscribe.complete();    
     
      if (this.unsubscribe.closed) {
        //subscribed
        console.log("unsubscribed");
      } else console.log("subscribed");
  }
}

and it logs to the console "subscribed".

the only operator I'm using in all of the pipes is 'takeUntil'.

Should I use '.unsubscribe' instead of '.complete'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文