If you want to load routes dynamically, you can utilize the resetConfig
method in Angular:
constructor(router:Router, http:HttpClient) {
this.http.get('/routes').subscribe(data=> {
this.router.resetConfig(data);
});
}
Remember to include any dynamically loaded components in the entryComponents
section of your app module:
entryComponents: [MyDynamicComponent,...]
Alternatively, if you have all your routes stored in an array that you fetch from a database, you can also add them to the entryComponents
array using the providers
array during the bootstrap process:
@NgModule({
...
providers: [
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, multi: true, useValue: routes }
]
})