javascript - 通过数组在 Angular 6 中引导多个模块

标签 javascript angular typescript frontend angular6

是否可以声明一个数组并使用它来引导 module.ts 中的多个组件。 我正在尝试这样的事情

    export var initialComponents = [];
    initialComponents.push(AppComponent);
    if(condition) {
      initialComponents.push(IchFooterComponent);
    }

然后

bootstrap: initialComponents

这给了我以下错误

Error: The module oa was bootstrapped, but it does not declare "@NgModule.bootstrap" components nor a "ngDoBootstrap" method. Please define one of these.

最佳答案

您可以通过将 ngDoBootstrap 实现为 AppModule 的方法来自定义引导。 您可以在 @NgModule

entryComponents 属性中列出需要引导的组件
@NgModule({
    entryComponents: [AComponent, BComponent, ...]
    ...
 })
 export class AppModule {

     constructor() {}

 ngDoBootstrap(appRef: ApplicationRef) {
     if (Math.random() > 0.5) {
         appRef.bootstrap(AComponent, '#app');
     } else {
         appRef.bootstrap(BComponent, '#app');
     }
 }

如果您需要服务,您可以通过依赖注入(inject)来访问它们(将其作为构造函数参数放入 AppModule 中)。但我不知道它与组件或服务中的 DI 相比是否有任何限制。 以下是 ApplicationRef 的文档及其引导方法。

关于javascript - 通过数组在 Angular 6 中引导多个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51259489/

相关文章:

javascript - typescript :(中间值)。(...)从派生类调用父类的方法时不是函数

javascript - 如何向现有对象添加元素

javascript - 将 Uint8ClampedArray 缓冲区输出为字节流

javascript - 如何在 JavaScript 中使用货币和相对于语言环境来格式化一个大数字(由字符串表示)?

javascript - 如何使用(this)访问Angular 2 http rxjs catch函数中的对象属性

typescript - 如何添加自定义类型定义?

javascript - 人类可读持续时间的格式? JavaScript

html - Angular 插值的 span 标签绑定(bind)问题

angular - 在 Angular 6 中使用网络套接字

javascript - 即使页面刷新也永久更改数组