简体   繁体   中英

Angular 2: Observables destroy itself when navigating to another route

I have a question regarding observables and its subscription. Would an observable subscription destroy itself when navigating away from the page where the subscription is initiated?

Also, if resolvers are returning data from an observable, would I need to unsubscribe from that observable? I use the resolver data through the following syntax

    this.resolverData = this.activatedRoute.snapshot.data['resolver1'];

1) Yes, if you want to persist observable data between different states you need to include it in a service and inject that service into the relevant app module (higher on the component tree if you want more components to know about it etc)

2) If you want to end an observable stream intentionally, you can try the dispose method, or if you want to trigger an event when the observable stream ends or throws an exception, you can use the finally method.

I hope this answers both of your questions :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM