是否有必要取消订阅 Angular Form statusChanges/valueChanges observable? [英] Is it necessary to unsubscribe Angular Form statusChanges/valueChanges observable?
问题描述
通过深入研究角度形式的实现,我发现可观察到的角度形式 valueChanges/statusChanges 是 EventEmitters.是否有必要取消订阅这些 observable?因为当单独使用 EventEmitters 时,我们不会取消订阅?
By dig into the angular form implementation I found the angular form valueChanges/statusChanges observable are EventEmitters. Is it necessary to unsubscribe these observables? since when use EventEmitters alone we don't unsubscribe?
_initObservables() {
(this as{valueChanges: Observable<any>}).valueChanges = new EventEmitter();
(this as{statusChanges: Observable<any>}).statusChanges = new EventEmitter();
}
推荐答案
如果不再需要通知,取消订阅
始终是一个好习惯.大多数情况下,您不一定可以放入 ngDestroy()
函数.
It is always a good practice to unsubscribe
if notification is no longer required. Most of the time you can put into ngDestroy()
function not necessarily.
是否总是需要取消订阅?不,如果 EventEmitter
或 Subject
或 Observable
在 Subscription
所在的相同范围(组件或服务)中,则没有需要销毁,因为所有的所有引用都将被完全删除.
Is it always required to Unsubscribe ?
No, If EventEmitter
or Subject
or Observable
in same scope (Component or Service ) where Subscription
is, then no need to destroy since all reference for all will be removed altogether.
简而言之,如果 Subscription
和 Observable
的范围不同,请确保 Observable
是 unsubscribe
.
In short, make sure Observable
is unsubscribe
if scope are different for Subscription
and Observable
.
这篇关于是否有必要取消订阅 Angular Form statusChanges/valueChanges observable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!