我正在尝试根据我在哪个区域运行我的应用程序来注册一组不同的路由。
该区域位于一个 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 serve
或 ng serve --aot
运行应用程序时,它工作得很好,但是当我尝试使用 ng build --prod --aot
,我一直收到以下错误:
ERROR in Cannot read property 'loadChildren' of null
我可以根据环境更改加载路线的方式,但到目前为止我尝试的所有方法都给出了类似的结果。
谢谢
最佳答案
您是否尝试过将阶段从“开发”切换到“构建”?
关于Angular 6 - 如何根据变量的值注册不同的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58852753/