【问题标题】:Is it possible to prevent the destruction of Angular components when navigating?导航时是否可以防止破坏Angular组件?
【发布时间】:2017-07-05 02:07:42
【问题描述】:

假设我有两个 Angular 2 组件:ComponentAComponentB。 我希望能够从 ComponentA 导航到 ComponentB,然后最终回到 ComponentA,而不必重新初始化 ComponentA。

在当前的 Angular 2 路由器实现中,每次我导航离开一个组件时,该组件都会被销毁,并且必须在下次导航到它时重新创建。

我知道我可以通过使用服务来保留组件的状态,但这似乎更像是一种解决方法,而不是我的问题的实际解决方案。有没有办法解决这个问题?

【问题讨论】:

  • 您好,君特。您的链接提到了CanReuse,但我在文档中找不到它。你知道它是否仍然存在,或者它是否已被重命名?
  • @AngularFrance 显然 CanReuse 已从 Angular 2 的最终版本中删除。为了达到类似的结果,我们可以使用类 RouteReuseStrategy,正如 Günter 的更新答案中所述。
  • 哈,谢谢! angular.io 上的搜索字段在搜索“重用”(不是事件RouteReuseStrategy)时没有产生任何结果,所以我找不到它。

标签: javascript angular typescript angular-router


【解决方案1】:

是的,是的,你需要实现RouteReuseStrategy

并为重用组件的路由定义一种行为。

在这个答案中查看更多信息:https://stackoverflow.com/a/41515648/13112018

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2017-04-28
    • 1970-01-01
    • 2018-12-23
    相关资源
    最近更新 更多