导航时是否可以防止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 路由器实现中,每次我离开一个组件时,该组件都会被销毁,并且必须在我下次导航到它时重新创建.
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?
推荐答案
是的,是的,需要实现 RouteReuseStrategy.
Yes, it is, you need to implement RouteReuseStrategy.
并为重用组件的路由定义行为.
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屋!