Skip to content

Angular2 Routing: persisting route tabs and child routes #6634

@Namek

Description

@Namek

(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:

  1. instantiate components only once - or better - decide when I want to do this (some Strategy?)
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerfeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationfreq2: medium

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions