【发布时间】:2020-10-28 09:07:33
【问题描述】:
我开始使用一个使用 Rxjs observables 的遗留 angular 6 应用程序。组件中的可观察对象似乎没有使用我所看到的取消订阅。 ngOnInit 中的订阅示例:
this.service.getCustomerDetail(customers).subscribe(
(data) => {
this.customerData = data;
},
() => {
this.notify.addNotification(
new notification(
`Error retrieving customer data`
)
);
}
);
订阅者正在使用完成,即使其中没有任何内容,即 Rxjs 订阅的 Next、Error、Complete 值,但在我看来,它需要将这些推送到订阅中,然后在 ngOnDestroy 上取消订阅所有这些。即使完整在可观察对象内,这些没有取消订阅的订阅也会留在堆内存中,也就是导致内存泄漏,这是正确的吗?
在我的订阅使用错误和完整部分的情况下,我是否会使用 TakeUntil 方法,例如:
this.service.getCustomerDetail(customers)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(
(data) => {
this.customerData = data;
},
() => {
//error section
this.notify.addNotification(
new notification(
`Error retrieving customer data`
)
);
},
() => {
//complete section
this.loadAddlDetail();
}
);
感谢您提供这方面的任何信息。
【问题讨论】:
-
takeUntil 方法是首选的角度解决方案stackoverflow.com/questions/38008334/…
-
@AdrianBrand 在描述中添加了一个问题,例如如果我已经有一个错误并完成订阅部分,我会使用 takeUntil 吗?