想检查一下我们如何在数据库中存储路由。例如
const routes: Routes = [
{
path: 'url-of-component
component: ABCComponent
}
我们如何将ABCComponent存储到mySQL DB中?或者,如何根据组件的路径解析组件(例如/component/abc.component.ts)
您可以使用resetConfig
动态加载路由:
constructor(router:Router, http:HttpClient) {
this.http.get('/routes').subscribe(t=> {
this.router.resetConfig(t);
});
}
确保将任何动态加载的组件添加到app模块的entryComponents
:
entryComponents: [MyDynamicComponent,...]
或者,如果您拥有可以从数据库中检索的数组中的所有路径,则可以通过entryComponents
数组将它们全部添加到providers
,作为引导过程的一部分:
@NgModule({
...
providers: [
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, multi: true, useValue: routes }
]
})