简体   繁体   English

角度2:可观察对象在导航到另一条路线时会自行毁灭

[英]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) 1)是的,如果要在不同状态之间保留可观察的数据,则需要将其包含在服务中并将该服务注入到相关的应用程序模块中(如果希望更多的组件知道该信息,则在组件树中位于较高的位置)

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. 2)如果要有意结束可观察流,可以尝试使用dispose方法,或者如果要在可观察流结束或引发异常时触发事件,则可以使用finally方法。

I hope this answers both of your questions :) 我希望这能回答您的两个问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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