Angular 2: Swapping between different components without destroying them

后端 未结 2 1721
不知归路
不知归路 2020-12-28 17:42

I\'m trying to build a window manager with Angular 2. Basically, I want to be able to swap between windows (that are components injected in a router outlet) without losing t

相关标签:
2条回答
  • 2020-12-28 18:24

    Checkout canReuse on the router: canReuse in angular2 api docs

    If you specify it the router wont destroy your component when switching away from it.

    0 讨论(0)
  • 2020-12-28 18:31

    Here's how I surpassed this requirement.

    Once the route changes, the windows components are destroyed. So, to save state in between those windows instances, I had to manually store it into an injected service.

    To store and load the state for each window component, I used the life-cycle hooks that are provided by the router: onActivate(next, prev) and onDeactivate(next, prev).

    EDIT

    Meanwhile, I've opened an issue on Angular 2 github repo requesting this feature.

    0 讨论(0)
提交回复
热议问题