angular - RXJS 使调用同步
问题描述
在我的服务中,我有一个从 iframe 父级获取令牌并使用该令牌进行休息调用的代码。
下面是执行 GET REST 调用的方法的代码片段。
public get_agencyschema() {
return this.tokenService.authenticatedToken.pipe(
map(token => {
const opt = {
headers: new HttpHeaders({
Authorization: token
})
};
console.log('get_agency %%%%%%%%%%%%%%%%%%%%%%%% ' + token);
return this.http.get(this.baseUrl', opt);
}));
}
当我从我的组件调用这个方法时,我得到一个未定义的。这是我的组件的代码。
private doSomething() {
this.rest.get_agencyschema().subscribe(data => {
/* tslint:disable:no-string-literal */
this.ccConfig = data['_embedded']['item'];
/* tslint:enable:no-string-literal */
for (let index = 0; index < Object.keys(this.ccConfig).length; index++) {
if (Object.keys(this.ccConfig[index].configuration).length > 0) {
this.Schema = this.ccConfig[index];
// console.log(this.Schema);
break;
}
}
// console.log(this.Schema.configuration);
this.LocationVerifyForm.setValue({
MatchDistance: this.Schema.configuration.MatchDistance,
IsReturnClosestMatch: this.Schema.configuration.IsReturnClosestMatch,
WildcardCharacters: this.Schema.configuration.WildcardCharacters,
IntersectionDelimiterCharacters: this.Schema.configuration.IntersectionDelimiterCharacters,
MaxReturnResults: this.Schema.configuration.MaxReturnResults,
IsDisplayHighAndLowCrossStreets: this.Schema.configuration.IsDisplayHighAndLowCrossStreets,
IsDisplayHighAndLowCrossBlocks: this.Schema.configuration.IsDisplayHighAndLowCrossBlocks,
IsDisplayBlockNumbersForIntersections: this.Schema.configuration.IsDisplayBlockNumbersForIntersections,
DisableAutoSelectVerifiedLocation: this.Schema.configuration.DisableAutoSelectVerifiedLocation,
IsDisplayTableOfContent: this.Schema.configuration.IsDisplayTableOfContent,
DistanceInMeter: this.Schema.configuration.DistanceInMeter,
Direction: this.Schema.configuration.Direction,
IsFilterBeatsByCityEnabled: this.Schema.configuration.IsFilterBeatsByCityEnabled,
CascadeLevel: this.Schema.configuration.CascadeLevel,
BlockKeyword: this.Schema.configuration.BlockKeyword,
BlockDisplayFormat: this.Schema.configuration.BlockDisplayFormat,
BlockGeoCodeLocation: this.Schema.configuration.BlockGeoCodeLocation,
IsRetainInvalidPremiseNumber: this.Schema.configuration.IsRetainInvalidPremiseNumber,
GeoDatabase: this.Schema.configuration.GeoDatabase
});
});
this.LocationVerifyForm = new FormGroup({
MatchDistance: new FormControl(),
IsReturnClosestMatch: new FormControl(),
WildcardCharacters: new FormControl(),
IntersectionDelimiterCharacters: new FormControl(),
MaxReturnResults: new FormControl(),
IsDisplayHighAndLowCrossStreets: new FormControl(),
IsDisplayHighAndLowCrossBlocks: new FormControl(),
IsDisplayBlockNumbersForIntersections: new FormControl(),
DisableAutoSelectVerifiedLocation: new FormControl(),
IsDisplayTableOfContent: new FormControl(),
DistanceInMeter: new FormControl(),
Direction: new FormControl(),
IsFilterBeatsByCityEnabled: new FormControl(),
CascadeLevel: new FormControl(),
BlockKeyword: new FormControl(),
BlockDisplayFormat: new FormControl(),
BlockGeoCodeLocation: new FormControl(),
IsRetainInvalidPremiseNumber: new FormControl(),
GeoDatabase: new FormControl()
});
}
我是 RXJS 和 Observables 的新手。有人可以帮我找出问题,以便在组件方法中获取 GET 方法的响应。
提前致谢。
解决方案
在 rxjs 中,你不会“映射”到一个新的 observable,但你会使用“switchMap”(如果总是只有一个调用可以激活)或“mergeMap”(如果多个执行可以并行运行)。
return this.tokenService.authenticatedToken.pipe(
switchMap(token => {
const opt = {
headers: new HttpHeaders({
Authorization: token
})
};
console.log('get_agency %%%%%%%%%%%%%%%%%%%%%%%% ' + token);
return this.http.get(this.baseUrl', opt);
}));
}
现在,您将返回将传递 http get 调用结果的 observable。
推荐阅读
- python-3.x - 如何使用flask检查每个文件夹是否有特定文件并使用jinja2显示结果?
- sql - 如何使用 SQL Server 创建算法
- python - Kolmogorov-Smirnov 解释两个信号之间的结果
- kubernetes - 在 Kubernetes 中,是否可以通过服务选择器中的元数据字段来引用 pod?
- css - Wordpress 主题更改样式表的顺序
- typo3 - 为什么我使用 TYPO3 V9.5 后会出现 404 错误
- maven - maven:存储库中的插件,但 mvn 没有找到它
- python - Django 视图:request.data 何时是 dict 与 QueryDict?
- linux - 在 MacOS 和 Linux 上检测输入 IDLE
- godot - Godot游戏引擎中的Mainloop是什么?