我有一个应用程序,其视图位于路由后面,我需要能够从路由更改时的位置继续,但返回后,组件处于初始状态。
有什么办法可以保持组件的状态吗?
update 2
现在已针对新路由器修复了这个问题(Angular 2.3)https://github.com/angular/angular/pull/13124 https://github.com/angular/angular/pull/13124它允许提供自定义重用策略。
有关示例,另请参阅https://www.softwarearchitekt.at/post/2016/12/02/sticky-routes-in-angular-2-3-with-routereusestrategy.aspx https://www.softwarearchitekt.at/post/2016/12/02/sticky-routes-in-angular-2-3-with-routereusestrategy.aspx
角度文档https://angular.io/api/router/RouteReuseStrategy https://angular.io/api/router/RouteReuseStrategy
update 2这个答案仅适用于很久以前停产的路由器版本。
See https://angular.io/docs/ts/latest/guide/router.html#!#guards https://angular.io/docs/ts/latest/guide/router.html#!#guards了解如何在当前路由器中执行此操作。
original
如果你的组件实现了CanReuse https://angular.io/docs/ts/latest/api/router/CanReuse-interface.html并返回true
from
routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) {
return true;
}
然后组件将被保留并重用,而不是被销毁和重新创建。
另一种方法是将数据保存在共享服务中,并在重新创建组件时从那里获取它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)