Confusion about checkProperties works on observable without subscription variable or not???? #172
-
In the example above, I understand that the subscription variable will unsubscribe automatically when the onDestroy method will call. but I want to know whether the code below works fine or not (unsubscribe automatically)????
or I Should use the old syntax like below?????
Thanks in Advance for you valueable time and helping.... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You should use the second example syntax, I’m guessing it’s a bit obvious that “checkProperties” option checks properties 😉 and in your example with ngOnInit there’s no property. |
Beta Was this translation helpful? Give feedback.
-
I think you could do this also: @UntilDestroy({ checkProperties: true })
@Component({})
export class InboxComponent {
ngOnInit() {
this.subscription = interval(1000).subscribe();
}
} The risk of this style is that if the ngOnInit method gets called a second time, you have a subscription hanging out somewhere in memory potentially closing over something with some mechanism to keep it alive and thus have a memory leak. On the other hand, this syntax: @UntilDestroy()
@Component({})
export class InboxComponent {
ngOnInit() {
interval(1000).pipe(untilDestroyed(this)).subscribe();
}
} is always safe. |
Beta Was this translation helpful? Give feedback.
You should use the second example syntax, I’m guessing it’s a bit obvious that “checkProperties” option checks properties 😉 and in your example with ngOnInit there’s no property.