延迟加载模块无法加载程序

lazy loading of my module fails program loading

提问人:user2554457 提问时间:11/5/2023 最后编辑:user2554457 更新时间:11/9/2023 访问量:59

问:

我正在尝试动态加载一个模块(取决于后端应用程序中的算法数量)。angular 应用程序会编译,但一旦我动态添加路由,就不会加载。 如果我静态(硬编码)添加它们,它们加载正常。

提前致谢! 相关代码片段如下:

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,
    },
  ],
}];

编辑:我是否错过了代码的重要部分?我似乎再也得不到答案了,我被困住了。

JavaScript Angular 延迟加载 Web 前端

评论


答:

0赞 gustavo henrique 11/5/2023 #1

您需要在 ROUTES 注入令牌中提供新的路由配置...在这种情况下,我认为您需要避免 forRoot() 或 forChild()

评论

0赞 Community 11/6/2023
您的答案可以通过其他支持信息进行改进。请编辑以添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心找到有关如何写出好答案的更多信息。
0赞 user2554457 11/7/2023
我还是新手,你说的ROUTES注入代币是什么意思?例如,您能提供一个代码片段吗?