angular - 在引导时将参数传递给我的 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'}]);
});
}
}
我相信有一种简单的方法可以实现它。有任何想法吗?
解决方案
我找到了解决方案。
@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 构造函数中。然后,在调用引导程序之前,我使用该服务设置了我想要的值。找不到其他东西。
推荐阅读
- vue.js - Vuejs v-show 没有按预期工作
- angular - 错误是 ERROR TypeError: Cannot read property 'FirstName' of undefined
- python - Decimal.quantize 舍入误差
- javascript - 在不是数组的可迭代对象上调用 Array.map 是否有效?
- java - 为什么 invokevirtual 的目标类型从 javac 目标 1.1 更改为 1.2?
- c# - System.Web.Mvc.WebViewPage
.Model.get 返回 null - javascript - 谷歌地图仅在我按 cmd+shift+R 时显示
- android - 为什么 `android:layout_height="0dp" ` 会产生与 XML 中的 `android:layout_height="wrap_content" ` 相同的结果
- arduino - 带有 NULL 字符的 Arduino HttpClient getString 问题。ESP32
- java - 一定时间重复后启用按钮