angular - Angular中的嵌套订阅-性能问题?
问题描述
我有一个subscription
存储和基于来自商店的价值,调用一个服务,我subscribe
会进一步。
嵌套订阅会导致性能问题吗?虽已毁onDestroy
。subscription
ngOnInit() {
this.currentYear = (new Date).getFullYear();
this.statesubscription = this.contentServerStore
.pipe(select(getContentServerState))
.subscribe(val => {
if (!this.checkValuesService.isNullOrUndefined(val) &&
val.region.toUpperCase() == Constants.region.us
) {
let contentRequest = new ContentRequest();
this.regionService.getRegion(contentRequest).subscribe(region => {
if (!this.checkValuesService.isNullOrUndefined(region)) {
this.regionContent = <string>this.domSanitizer
.bypassSecurityTrustHtml(region[Constants.regionKey]);
}
});
}
});
}
解决方案
尝试使用 flatMap 操作符来嵌套订阅。
ngOnInit() {
this.currentYear = (new Date).getFullYear();
this.statesubscription = this.regionService.getRegion(contentRequest).flatMap(region => this.contentServiceFunc(region)).subscribe()
}
private contentServiceFunc(region) {
return this.contentServerStore.pipe(select(getContentServerState))
.subscribe(val => {
if (!this.checkValuesService.isNullOrUndefined(val) &&
val.region.toUpperCase() == Constants.region.us
) {
let contentRequest = new ContentRequest();
if (!this.checkValuesService.isNullOrUndefined(region)) {
this.regionContent = < string > this.domSanitizer.bypassSecurityTrustHtml(region[Constants.regionKey]);
}
}
});
}
推荐阅读
- php - PHP中的MYSQL查询正在删除我表中的所有行
- c++ - 如何访问内部类中的类的静态成员?
- java - 如何检查 System.in 是否仅包含数字
- google-speech-api - 如何提高 Google Speech to Text API 中的标点和类标记的准确性
- angular - 在规范文件中运行多个测试时发出清理组件角度
- typescript - 打字稿和 Firebase 9
- javascript - 元素 scrollIntoView 在 Safari 中不起作用
- android - 如何在没有任何密码的情况下在 Flutter 中解密和删除 pdf 的有限权限?
- javascript - 如何创建以下样式的按钮?
- python - 如何在 Matplotlib 中打印带有标题的图像?