我有多个利用功能的组件。他们希望返回对象数组。但是,在一个实例中,我需要在返回之前检查可观察对象的返回值以更改数组的内容:
let projectMenu: MenuElement[];
...
this.myService.enabledFeatures.subscribe(features => {
if (features[FeatureKeys.SPECIAL_KEY]) {
projectMenu.push(this.getNewMenuItem(project));
projectMenu.push(this.getSecondaryMenuItem(project));
}
});
return projectMenu;
显然return projectMenu
,在订阅执行魔术之前,将返回其当前值。我基本上需要将其subscribe
转换为同步请求。也许通过异步等待?虽然不太确定如何去做。
您可以map
在发出以下信号之前更改值:
this.myService.enabledFeatures.pipe(
map((features) => {
let projectMenu: MenuElement[] = [];
if (features[FeatureKeys.SPECIAL_KEY]) {
projectMenu.push(this.getNewMenuItem(project));
projectMenu.push(this.getSecondaryMenuItem(project));
}
return projectMenu;
})
).subscribe()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句