why ActivatedRoute in @angular\router params is observable?

青春壹個敷衍的年華 提交于 2019-12-12 07:52:44

问题


why ActivatedRoute in @angular\router params is observable ?

I'm wondering why do I need to subscribe for Params ? and why params are async is there a case the componenet of the route will be created but the params still not populated ?

and is there a way to just get the value without an observable ?


回答1:


You can use

route.routerState.params

You can subscribe because when your application navigates to the same route with just another parameter value, the component is not recreated and you have no way of knowing when the parameters have changes.

With subscribing to parameters you get notified, that a route action has happened where only the parameter value changed.




回答2:


The matrix parameters scoped to this route. The observable will emit a new value when the set of the parameters changes.

The reason that the params property on ActivatedRoute is an Observable is that the router may not recreate the component when navigating to the same component. In this case the parameter may change without the component being recreated.



来源:https://stackoverflow.com/questions/40577118/why-activatedroute-in-angular-router-params-is-observable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!