简体   繁体   English

导航到使用相同组件的另一条路线时,组件的角度检索状态

[英]Angular Retrieve state of a component when navigating to another route that uses the same component

I have two different routes /a and /b using the same component. 我有两个不同的路由/a/b使用相同的组件。 When navigating from /a to /b , I'd like to be able to retrieve the state of the component instance used in /a to reuse it in /b . /a导航到/b ,我希望能够检索/a中使用的组件实例的状态以在/b重用它。

I read here https://github.com/angular/angular/issues/12446 that it is or will be possible using Angular "custom route reuse strategies". 我在这里https://github.com/angular/angular/issues/12446读到,使用Angular“自定义路由重用策略”已经或将有可能。 I am wondering if it's possible using the current RouteReuseStrategy ? 我想知道是否可以使用当前的RouteReuseStrategy Because it seems to be easy to reuse a component when going back to the same url but not when going to a different url using the same component. 因为在返回相同的URL时重用一个组件似乎很容易,但是在使用相同的组件访问不同的URL时却不容易。

I am not sure about your real time use case but you can use RouteReuseStrategy reuse the UI or Component state. 我不确定您的实时用例,但是可以使用RouteReuseStrategy重用UI或Component状态。 You have to generate a common unique key for both route and store that state when you leave the route. 您必须为路由生成一个通用的唯一密钥,并在离开路由时存储该状态。 Stored state can be re-used when route back with routes which has same key. 当路由具有相同密钥的路由返回时,可以重新使用存储状态。

In this https://plnkr.co/edit/wFPn16?p=preview 在此https://plnkr.co/edit/wFPn16?p=preview

  Visited component state reused when you visit to different route. For example view and edit item with id 12345. then check the view and> edit details for id 12346. View and edit component show the details related 12345. 

HTH HTH

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

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