导航时是否可以防止损坏Angular组件? [英] Is it possible to prevent the destruction of Angular components when navigating?
问题描述
假设我有两个Angular 2组件:ComponentA
和ComponentB
.
我希望能够从ComponentA导航到ComponentB,然后最终回到ComponentA,而不必重新初始化ComponentA.
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.
在当前的Angular 2 Router实现中,每次我离开某个组件时,该组件都会被破坏,并且下次我导航至该组件时必须重新创建该组件.
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.
我知道我可以通过使用Service来保留组件的状态,但这似乎比实际解决方案更像是一种解决方法.有什么办法解决吗?
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?
推荐答案
是的,您需要实现,并为重用组件的路由定义行为.
and define a behavior for routes that reuse components.
在此答案中查看更多信息: https://stackoverflow.com/a/41515648/13112018
Check more info in this answer: https://stackoverflow.com/a/41515648/13112018
这篇关于导航时是否可以防止损坏Angular组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!