Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: appending child routes dynamically

I would like to know if it is possible to dynamically append child routes at run time, from lets say a service...?

Given:

const routes: Routes = [
    {
        path: '', component: GloriousComponent,
        children: [
           { path: 'path1', component: Child1Component },
           { path: 'path2', component: Child2Component },
           { path: 'path3', component: Child3Component },
        ]
    }
];

Could I remove the children of the ' ' path and somehow get a reference to the const routes and then later on dynamically append children to the ' ' path?

Something along the lines of...

const routes: Routes = [
    {
        path: '', component: GloriousComponent           
    }
];

routes[''].appendChildren(
        [
           { path: 'path1', component: Child1Component },
           { path: 'path2', component: Child2Component },
           { path: 'path3', component: Child3Component },
        ]
    )
like image 804
Thibs Avatar asked Jan 06 '17 04:01

Thibs


1 Answers

Currently modifying is not supported but you can maintain a list of routes yourself and then call

this.router.resetConfig(routes)

to load a new set of routes into the router.

like image 54
Günter Zöchbauer Avatar answered Nov 11 '22 09:11

Günter Zöchbauer