I'm trying to set the router config(containing the allowed urls etc) based on data from a REST API. In this case, the url /test2 should end up at the HomeComponent. Url test2 is not present in the initial route config, it is added by a promise.
So far the approach I have used is the following:
RouterModule.forRoot([
{
path: '',
component: HomeComponent
},
{
path: '**',
resolve: {
route: RouteResolver
},
component: ErrorComponent
}
])
Where the Resolver is responsible for adding new routes.
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any>|Promise<any>|any {
return new Promise((resolve, reject) => { // just to fake api call
console.log(this.router.config);
this.router.config.push(
{
path: 'test2',
component: HomeComponent
}
);
this.router.resetConfig(this.router.config);
console.log(this.router);
resolve();
});
}
Problem with this approach it seems i'm one step too late. The new route does get added to the route config, and the config does get reset. But during the resolver execution the application doesn't seem aware of this yet. So I still end up at the ErrorComponent.
Any idea's how to get this done? Perhaps im searching in the wrong area for a solution :(