You have a component that fetches and displays user data. You want to ensure that if the component is destroyed, the subscription to the Observable fetching the data is also terminated. How would you achieve this?
- Manually unsubscribing in ngOnDestroy()
- Using a takeUntil() operator with ngOnDestroy()
- Using a timeout operator with RxJS complete()
- Using async pipe with RxJS finalize() operator
To ensure the subscription is terminated when the component is destroyed, you can use the takeUntil() operator in combination with ngOnDestroy(). This allows you to unsubscribe when the component's lifecycle ends, preventing memory leaks.
Loading...
Related Quiz
- How can you implement a custom form control that works seamlessly with Form Groups and Form Arrays?
- Which RxJS operator can be used to combine multiple Observables into a single Observable?
- To navigate to a route conditionally based on the data fetched, you can use a Route Resolver along with the _____ Router Event.
- When defining routes, the _____ property specifies which component should be displayed when a particular path is navigated to.
- To ensure proper cleanup and prevent memory leaks, it's important to unsubscribe from observables when they are no longer needed.