首页 > 解决方案 > 不能在 Angular 路由中使用枚举

问题描述

为避免路径或导航出现拼写错误,在具有许多路径的深度嵌套应用程序中,尝试使用枚举或常量作为路径路径

export enum AppRoutes {
    MAIN = 'mainView',
    MAIN_SUB = 'mainView/SubView'
}
const registrationRoutes: Routes = [
    {
      path: AppRoutes.MAIN,
      component: ..
      children: [
        {
          path: '',
          component: ..
        },
        {
          path: AppRoutes.MAIN_SUB.substr(AppRoutes.MAIN_SUB.indexOf('/') + 1),
          component: ..
        },
        {
          path: `${AppRoutes.MAIN_SUB.substr(AppRoutes.MAIN_SUB.indexOf('/') + 1)}`,
          component: ..
        }
      ]
    }
  ];

这在没有启用的情况下可以很好地工作ng serve并编译代码。aot但是,使用production代码或aot启用它总是会导致path编译器省略 where 属性。

{
    path: "",
    component: A
    children: [{
        path: "",
        component: sn
    }, {
        component: Pn
    }, {
        component: An
    }]
}

我曾尝试使用enum, const,等static属性,component但结果始终相同。

试图找出代码有问题还是AOT行为......

标签: angulartypescriptroutesaot

解决方案


推荐阅读