Angular 6:如何根据配置添加模块?

时间:2018-05-17 15:54:57

标签: angular webpack angular-cli

到目前为止我做了什么(以及什么工作正常)

我有一个由多个不同客户使用的角度应用程序,我目前正在从角度4更新为角度6.客户有自己不同的扩展,因此在角度4中我为每个客户创建了一个模块(设置自定义)然后我为每个客户创建了一个environment.CUSTOMER_NAME.ts文件,内容如下:

export const environment: Environment = {
    extraModules: [MODULE_FOR_THIS_CUSTOMER]
};

然后在我使用的主要模块中:

@NgModule({
    imports: (<Array<Type<any> | ModuleWithProviders | any[]>>[
        BrowserModule,
        FormsModule,
        /* .. */
    ].concat(environment.extraModules)),
    /* .. */
})
export class AppModule { }

这很有效。运行ng build --env=SOME_CUSTOMER构建整个应用程序,包括该特定客户的模块。

问题

现在我升级到了角度6,我收到了错误:

ERROR in : 'router-outlet' is not a known element:
1. If 'router-outlet' is an Angular component, then verify that it is part of this module.
2. If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<router-outlet></router-outlet>")

经过一天白发并考虑辞掉工作并成为公交车司机后,我终于想通了,我的concat - 命令似乎是问题的根源。至少它与选项

结合使用
"aot": true,
"buildOptimizer": true,
<{1>}文件中的

。如果我把它们都设置为假,那么一切都运行正常,即使是我的旧结构。删除angular.json命令也可以解决问题。

当然,我真的不想关闭这些选项,那么只有在某个配置处于活动状态时,我还能做什么才能包含模块?

2 个答案:

答案 0 :(得分:1)

它说:

ERROR in : 'router-outlet' is not a known element:

我想你可能在你的客户模块中使用路由器插座。您需要确保在模块中导入路由器模块。

答案 1 :(得分:0)

虽然concat调用不再可行,但在使用自版本2.1以来TypeScript也支持的the spread-operator ...时它可以正常工作:

@NgModule({
    imports: [
        /* .. */
        ...environment.extraModules
    ]
})