首页 > 解决方案 > 在引导时将参数传递给我的 AppComponent

问题描述

我正在开发一个托管在 asp.net 应用程序上的 angular v7 应用程序。基本上我将在我的 .net 应用程序上有一个下拉菜单,我想用选定的值调用角度。我做了几次尝试在引导程序(app.module.ts)中传递一个参数,但我得到了一个错误。

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  entryComponents: [AppComponent]
})
export class AppModule { 

  ngDoBootstrap(app: ApplicationRef) {
    document.addEventListener('myAppA', function(e : CustomEvent) {
        app.bootstrap(AppComponent, [{provide:'MESSAGE', useValue: 'Hello Angular'}]);
    });
  }
}

在此处输入图像描述

我相信有一种简单的方法可以实现它。有任何想法吗?

标签: angular

解决方案


我找到了解决方案。

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
    ConfigService
  ],
  entryComponents: [AppComponent]
})
export class AppModule implements DoBootstrap { 
  constructor(private config: ConfigService) { }

  ngDoBootstrap(app: ApplicationRef) {
    const self = this;
    document.addEventListener('myAppA', function(e : CustomEvent) {
      self.config.setScheme("BBB");
      app.bootstrap(AppComponent);
    });
  } 
}

基本上我创建了一个服务并将它注入到 AppModule 构造函数中。然后,在调用引导程序之前,我使用该服务设置了我想要的值。找不到其他东西。


推荐阅读