简体   繁体   中英

Is it possible to prevent the destruction of Angular components when navigating?

Assuming I have two Angular 2 components: 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 component, the component gets destroyed and has to be recreated the next time I navigate to it.

I know that I can preserve the state of components 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?

Yes, it is, you need to implement RouteReuseStrategy .

and define a behavior for routes that reuse components.

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

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