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 classRouteReuseStrategy
, 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
1 Answer
Reset to default 2Yes, 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
版权声明:本文标题:javascript - Is it possible to prevent the destruction of Angular components when navigating? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742333495a2455162.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论