简体   繁体   English

如何在 Angular 中一次取消订阅一个组件中的所有 observables

[英]How to unsubscribe from all observables in one component at once in Angular

In my angular project, I have multiple subscriptions in a few components.在我的 angular 项目中,我在几个组件中有多个订阅。 So I use properties inside of it to store subscriptions, and in destroy function, I unsubscribe all of them.所以我使用其中的属性来存储订阅,并在销毁 function 时,我取消了所有订阅。 The problem is that the code becomes larger unnecessary larger.问题是代码变得更大不必要更大。 My question is, is there any way to unsubscribe all subscriptions at ones?我的问题是,有没有办法取消订阅所有订阅?

I tend to store all my subscriptions in an array..我倾向于将所有订阅存储在一个数组中。

private subs: Subscription[] = []

ngOnInit() {
  this.subs.push(
     this.obs1.subscribe(),
     this.obs2.subscribe() // etc
  )
}

ngOnDestroy() {
  this.subs.forEach(s => s.unsubscribe())
}

other people prefer other methods.其他人更喜欢其他方法。 just be creative.只要有创意。

A recommanded way to unsubscribe to an observable is to call取消订阅 observable 的推荐方法是调用

ngOnDestroy () {
 this.obs.next();
 this.obs.complete();
}

To do it for all observable at once, you can declare a subject and use takeUntil from rxjs:要一次对所有可观察对象执行此操作,您可以声明一个主题并使用takeUntil中的 takeUntil:

ngUnsubscribe = new Subject();

... code...

ngOnInit() {
  this.myObs.pipe(takeUntil(this.ngUnsubscribe))
  .subscribe(_ => ...);
}

ngOnDestroy() {
  this.ngUnsubscribe.next();
  this.ngUnsubscribe.complete();
}

I do it this way because I noticed that in my project, calling .unsubscribe() wasn't working as expected.我这样做是因为我注意到在我的项目中,调用.unsubscribe()没有按预期工作。 Switching from component A to B and then back to A created a second (then third, etc.) subscription and finally fixed by applying advice from this thread ).从组件 A 切换到 B 然后再切换回 A 创建了第二个(然后是第三个等)订阅,最后通过应用来自该线程的建议来修复)。

There is many ways to do that but we can't avoid the boilerplate, you can use the ways above or use a decorator for that but keep in mind that there are exceptions where you don't have to unsubscribe:有很多方法可以做到这一点,但我们无法避免样板文件,您可以使用上述方法或使用装饰器,但请记住,有些例外情况您不必取消订阅:

No Need of unsubscribe in these cases在这些情况下无需取消订阅

  1. In case of HttpClient calls because the observable emit one value (success or error) and complete automatically.在 HttpClient 调用的情况下,因为 observable 发出一个值(成功或错误)并自动完成。

  2. In case or ActivatedRoute subscription because the Router will destroy it when the component is destroyed automatically万一或ActivatedRoute订阅,因为Router会在组件自动销毁时销毁它

  3. use of Async pipe使用异步 pipe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何取消订阅角度组件中的多个可观察对象? - How to unsubscribe multiple observables in angular component? 我应该取消订阅根 Angular 组件中的 observables 吗? - Should I unsubscribe from observables in root Angular component? Angular Observables 异步取消订阅 pipe - Angular Observables Unsubscribe async pipe angular2 / rxjs ngUnsubscribe:如何使用Decorator取消订阅所有订阅? - angular2/rxjs ngUnsubscribe: how to unsubscribe from all subscriptions with decorator? 如何通过一键单击以角度订阅取消订阅? - How to subscribe unsubscribe in angular from one button click? 是否有必要取消订阅Angular组件中的Redux存储 - Is it necessary to unsubscribe from a Redux store in an Angular component 在 rxjs 中,如何使用 takeUntil 或 takeWhile 取消订阅“内部”“间隔”可观察量 - In rxjs, how to unsubscribe from "inner" "interval" observables using takeUntil or takeWhile 如何取消订阅 Angular 2 中的 EventEmitter? - How to unsubscribe from EventEmitter in Angular 2? 在http调用的情况下Angular 2订阅/取消订阅Observable - Angular 2 Subscribe / Unsubscribe Observables in case of http calls Angular 4,Observables和“无法读取null的属性'unsubscribe'” - Angular 4, Observables, and “Cannot read property 'unsubscribe' of null”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM