-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Description
(I posted the problem on SO: http://stackoverflow.com/questions/34925782/angular2-routing-persisting-route-tabs-and-child-routes but decided to ask here, too, hopefully more specific this time)
So, basically Router in Angular 2 destroys inactive components (my tabs!). The problem is I don't want this behaviour. Reason is I have some components like charts and data grid and want to switch between them fast, I don't want to recreate them.
I've found some workaround for one-level routing but it's totally not a solution.
app.ts:
@Component({ /*...*/ })
@RouteConfig([
{path: '/**', redirectTo: ['Dashboard']},
{path: '/dashboard', name: 'Dashboard', component: EmptyRoute},
{path: '/products/...', name: 'Products', component: EmptyRoute},
{path: '/sales', name: 'Sales', component: EmptyRoute},
{path: '/reports', name: 'Reports', component: EmptyRoute},
])
export class App {
constructor(private router: Router) {
}
public isRouteActive(route) {
return this.router.isRouteActive(this.router.generate(route))
}
}As you can see component is set to EmptyRoute (just an empty view @Component with no methods and no data) and I don't use <router-outlet> but instead I instantiate my route components manually:
app.html:
<dashboard [hidden]="!isRouteActive(['/Dashboard'])"></dashboard>
<products-management [hidden]="!isRouteActive(['/Products'])"></products-management>
<sales [hidden]="!isRouteActive(['/Sales'])"></sales>
<reports [hidden]="!isRouteActive(['/Reports'])"></reports>but of course it doesn't work for child routes. In my (existing - written with Angular 1.x) application I have /products/product/21/pricing or /products/product/21 etc. (could get rid of products/ part in the middle but whatever).
So, going back to Router. It instantiates and destroys. Ideally, I'd like to:
- instantiate components only once - or better - decide when I want to do this (some Strategy?)
- decide whether to destroy or hide components. When I hide a component I want to reuse it when route switches to the component.
EDIT on 14.11.2017: some people ask me about solution - yeah, I ditched Angular and use Elm for private projects and changed my job.