【发布时间】:2017-08-01 05:22:48
【问题描述】:
我订阅了一项服务,我从外部 API 请求图像 URL 并将其存储在共享服务中。
逻辑:
1.清空当前图像数组
2.从API请求图片,并推送到(新清除的)数组(response =>)
3.如果URL返回错误,用默认(占位符)图像填充数组(错误=>)
由于某种原因,尽管存在错误,但仍未处理该错误。 我已经发布了我的组件和错误消息以获取上下文。
组件.ts
getImages(vehicleID: number, color: string): void {
this._carConfigService.car.colors.exterior.urls = [];
this._FAPIService.getImagesForColors(vehicleID, color)
.subscribe(
response => {
response.products[0].productFormats.forEach(pF => {
this._carConfigService.car.colors.exterior.urls.push(pF.assets[0].url);
});
},
error => {
this.errorMessage = <any>error;
if (this._carConfigService.car.colors.exterior.urls.length === 0) {
this._carConfigService.car.colors.exterior.urls = this._carConfigService.defaultImages;
}
});
}
我收到以下错误:
Failed to load resource: the server responded with a status of 422 ()
EXCEPTION: Response with status: 0 for URL: null
Uncaught Response
【问题讨论】:
-
您可以使用编写服务的
onCatch方法处理错误,然后在映射后将其抛出,然后您的订阅者将能够处理它 -
@BabarBilal,这可能是我正在寻找的解决方案,您可以发布示例代码 sn-p 以便我将您的答案标记为正确吗?谢谢:)
-
我来自手机无法发布代码 sn-p 添加了 2 个链接都可以解决您的问题,祝您好运。
-
请添加您的服务并提出问题
标签: angular angular2-services angular2-components angular2-observables