完成的行为主题

huangapple go评论71阅读模式
英文:

Complete BehaviorSubject

问题

I’m using BehaviorSubject and subscribe to it using async pipe, do I need to complete the BehaviorSubject on component destroy?
*我不在 .ts 文件中订阅它

英文:

I’m using BehaviorSubject and subscribe to it using async pipe, do I need to complete the BehaviorSubject on component destroy?
*I don’t subscribe to it on the .ts file

答案1

得分: 4

complete() 有两个作用:

  1. 向其订阅者发送完成信号,触发它们的完成回调。
  2. 取消所有当前的订阅者并阻止新的订阅。

所以,如果你只在组件中使用 BehaviorSubject,并且只通过 async 管道使用它,它会自动管理其底层订阅(在模板执行时开始订阅,在组件即将销毁时停止订阅),那么你不需要调用 complete()

然而,如果这个 BehaviorSubject 存在于一个共享服务中,或者被多个订阅者使用,调用 complete() 可能是一个错误,因为它会为所有订阅者完成,而不仅仅是你的组件。或者根据你的需求,如果你确实想完全清理 BehaviorSubject,那么可能确实需要调用它。

总之,我认为在以下情况下调用 complete() 是有意义的:

  • 在 Subject 被共享的地方,或者你关心完成事件时,
  • 在 Subject 是“私有”的情况下不需要调用它,你不关心完成事件,并确保取消所有你的订阅,尽管听起来有很多条件,但通常对大多数组件来说都是这样的情况。
英文:

complete() does 2 things:

  1. sends a completion signal to its subscribers, which triggers their completion callbacks
  2. unsubscribes all current subscribers and prevents new ones from subscribing

So if you only use the BehaviorSubject in your component, and only use it via async pipe, which manages its underlying subscriptions automatically (starts the subscription as soon as its template get executed, stops the subscription when the component is about to be destroyed), you don't have to call complete().

If however this BehaviorSubject exists in a shared service and/or is used by multiple subscribers, calling complete() might either be a mistake, because you would complete it for all subscribers, not just your component. Or, depending on your needs, it might be actually needed if you indeed want to clean up the BehaviorSubject completely.


To sum it up, I think it makes sense to

  • call complete() where the Subject is shared, or you care about the completion event, and
  • not to call it where the Subject is "private" to your class, you don't care about the completion event and you make sure you unsubscribe all your subscriptions, which sounds like a lot of conditions but is usually the case for most components

答案2

得分: 0

如果您在使用async管道访问行为主题的情况下,在特定组件被销毁时,您不需要手动取消订阅该主题到您的.ts文件中,Angular将自动为您取消订阅。

然而,如果您在您的组件.ts文件中手动订阅主题,那么只有在这种情况下,您应该在组件的ngOnDestroy()生命周期钩子中手动取消订阅,以避免内存泄漏错误。

希望您得到了答案,
谢谢!

英文:

If you are accessing behavior subject with async pipe in that case you do not need to manually unsubscribe that subject into your .ts file, angular will automatically unsubscribe for you when specific component is destroyed.

However, if you are manually subscribing subject into your component .ts file only in that case you should have to manually unsubscribe from your component ngOnDestroy() life cycle hook to avoid memory leaks errors.

Hope you get your answer,
Thanks!

答案3

得分: 0

你可以在 ngOnDestroy() 中使用 complete()

ngOnDestroy() {
  this.subject.complete();
}
英文:

you could use complete() in ngOnDestroy()

ngOnDestroy() {
 this.subject.complete();
}

huangapple
  • 本文由 发表于 2023年5月11日 12:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定