admin管理员组

文章数量:1332339

Assuming I have two Angular 2 ponents: ComponentA and ComponentB. I want to be able to navigate from ComponentA to ComponentB and then eventually back to ComponentA, while not having to reinitialize ComponentA.

In the current Angular 2 Router implementation, every time I navigate away from a ponent, the ponent gets destroyed and has to be recreated the next time I navigate to it.

I know that I can preserve the state of ponents by using a Service, but this seems to be more like a workaround than an actual solution to my issue. Is there any way around this?

Assuming I have two Angular 2 ponents: ComponentA and ComponentB. I want to be able to navigate from ComponentA to ComponentB and then eventually back to ComponentA, while not having to reinitialize ComponentA.

In the current Angular 2 Router implementation, every time I navigate away from a ponent, the ponent gets destroyed and has to be recreated the next time I navigate to it.

I know that I can preserve the state of ponents by using a Service, but this seems to be more like a workaround than an actual solution to my issue. Is there any way around this?

Share Improve this question edited Aug 12, 2018 at 12:02 Gil Epshtain 9,86110 gold badges74 silver badges98 bronze badges asked Feb 16, 2017 at 5:43 Tudor CiotlosTudor Ciotlos 1,8455 gold badges30 silver badges47 bronze badges 6
  • 1 See the links in stackoverflow./questions/33940095/… – Günter Zöchbauer Commented Feb 16, 2017 at 5:45
  • 1 See also stackoverflow./questions/41280471/… – yurzui Commented Feb 16, 2017 at 5:48
  • Hi Günter. Your link mentions CanReuse but I couldn't find it in the docs. Do you know if it still exists or if maybe it has been renamed? – AngularChef Commented Feb 16, 2017 at 9:18
  • @AngularFrance Apparently CanReuse was removed from the final version of Angular 2. To achieve a similar result, we can use the class RouteReuseStrategy, as also stated in the updated answer of Günter. – Tudor Ciotlos Commented Feb 16, 2017 at 10:27
  • Ha, thanks! The search field on angular.io didn't yield any results when searching "reuse" (not event RouteReuseStrategy) so I couldn't find it. – AngularChef Commented Feb 16, 2017 at 10:31
 |  Show 1 more ment

1 Answer 1

Reset to default 2

Yes, it is, you need to implement RouteReuseStrategy.

and define a behavior for routes that reuse ponents.

Check more info in this answer: https://stackoverflow./a/41515648/13112018

本文标签: javascriptIs it possible to prevent the destruction of Angular components when navigatingStack Overflow