我正在尝试动态加载模块(取决于后端应用程序中算法的数量)。角度应用程序会编译,但在我动态添加路线后不会加载。 如果我静态添加它们(硬编码),它们加载得很好。
提前致谢! 相关代码片段如下:
lab-routing.module.ts:
export const MLRoutes: Routes = [];
export const NormRoutes: Routes = [];
export const LAB_ROUTES: Routes = [{
path: '',
component: LabComponent,
children: [
{
path: 'Dashboard',
component: LabDashboardComponent,
},
{
path: '',
redirectTo: 'Dashboard',
pathMatch: 'full',
},
{
path: '**',
component: NotFoundComponent,
},
],
}];
lab-menu.ts:
export function insert_ml(algo: string): void {
ML_ITEMS.push({
title: algo,
icon: 'keypad-outline',
children: [
{
title: 'Datasets',
icon: 'activity-outline',
link: `/lab/${algo}/dataset`,
},
{
title: 'Results',
icon: 'bar-chart',
link: `/lab/${algo}/results`,
},
],
});
MLRoutes.push({
path: algo,
loadChildren: () => import('./ml-algo/ml-algo.module').then(m => m.MLAlgoModule),
});
}
lab-dashboard.component.ts:
public refresh() {
this.http.get('http://localhost:5000/refresh').subscribe(
data => {
if (data['success']) {
data['ml_algos'].forEach(algo => insert_ml(algo));
data['norm_algos'] .forEach(algo => insert_norm(algo));
build_menu();
}
},
);
}
ml-algo-routing.module.ts:
const routes: Routes = [{
path: '',
component: MLAlgoComponent,
children: [
{
path: 'dataset',
component: MLDatasetComponent,
},
{
path: 'results',
component: MLGraphsComponent,
},
],
}];
您需要在 ROUTES 注入令牌中提供新的路由配置...在这种情况下,我认为您需要避免 forRoot() 或 forChild()