Angular 6 - 如何根据变量的值注册不同的路由

标签 angular angular6 angular-routing

我正在尝试根据我在哪个区域运行我的应用程序来注册一组不同的路由。

该区域位于一个 JSON 文件中,该文件在构建应用程序后动态替换,我按以下方式将其加载到我的 environment.ts 文件中:

import config from '../assets/config/config.json';

export const environment = config;

然后环境变量包含类似的内容:

{
    "stage": "dev",
    "endpoint": "<backend endpoint>",
    "realm": "<US or CA>"
}

然后在我的一个模块中加载路线,但我想要根据区域设置不同的路线,因此我有以下代码:

export const routes: Routes = {
    US: [
        { path: 'my-route', component: MyComponentForUS }
    ],
    CA: [
        { path: 'my-route', component: MyComponentForCA }
    ]
}[environment.realm];

@NgModule({
    imports: [
        RouterModule.forChild(routes)
    ],
    declarations: [
        MyComponentForUS,
        MyComponentForCA
    ]
})
export class MyModule {
}

当我使用 ng serveng serve --aot 运行应用程序时,它工作得很好,但是当我尝试使用 ng build --prod --aot,我一直收到以下错误:

ERROR in Cannot read property 'loadChildren' of null

我可以根据环境更改加载路线的方式,但到目前为止我尝试的所有方法都给出了类似的结果。

谢谢

最佳答案

您是否尝试过将阶段从“开发”切换到“构建”?

关于Angular 6 - 如何根据变量的值注册不同的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58852753/

相关文章:

javascript - 在没有 url 帮助的情况下在 Controller 之间传递数据?

node.js - NPM 安装错误 :Unexpected end of JSON input while parsing near '...nt-webpack-plugin" :"0'

angular - 如何在 Angular 7 中使用路由器导航进行滚动?

angular - 如果至少有一个字段具有值,则向响应式(Reactive) FormGroup 的所有控件添加必需的验证

angular6 - 如何在角度模板驱动表单模型中使用字段集表示数组

angular - 禁用依赖日期选择器 Angular Power Bootstrap angular 6

angular6 - 如何在 Angular 6 的 index.html 中使用环境变量

angularjs - 在使用 routeProvider 在 Angular 中启动路由之前,如何检查登录或其他状态?

angular - 以 Angular 提交时显示表单验证错误消息

angular - 需要进行多次HTTP调用并合并结果