Angular 延迟可观察的时间
我正在处理一个场景,比如需要在应用程序中的每个Post调用之前调用一个伪get Api调用 注:1。实际的post调用应该在伪get调用完成后启动一次。 2.需要通过仅在服务文件中编辑代码来解决此问题 这是我的问题 组件技术Angular 延迟可观察的时间,angular,observable,Angular,Observable,我正在处理一个场景,比如需要在应用程序中的每个Post调用之前调用一个伪get Api调用 注:1。实际的post调用应该在伪get调用完成后启动一次。 2.需要通过仅在服务文件中编辑代码来解决此问题 这是我的问题 组件技术 this._service.dopost().subscribe( data => { console.log(data); }); 服务台 dopost() { this._http.get(dammyUrl, body, {
this._service.dopost().subscribe(
data => { console.log(data); });
服务台
dopost() {
this._http.get(dammyUrl, body, {
observe: "response",
headers: headers
}).subscribe((responce)=>{
return this._http.post(url, body, {
observe: "response",
headers: headers
});
});
}
堆栈闪电战链接
据我所知,在这种情况下你需要。见下例:
dopost() {
return this._http.get(dammyUrl, body, {
observe: "response",
headers: headers
}).pipe(flatMap(responce=>{
return this._http.post(url, body, {
observe: "response",
headers: headers
});
}));
}
您必须订阅问题中描述的dopost方法。您需要使用
映射到可观察,完成之前的内部可观察,发射值
您希望组件中有什么响应?@bryan60我想得到组件中调用后的响应-我已经按照您所说的方式进行了尝试。它在组件中引发异常。抱歉,我没有返回,我更新了响应。请看这里的stackblitz:太棒了。。谢谢当然只要记住ming@Narayanan,除非您不需要响应头,否则使用HttpClientModule比使用HttpModule更容易。将相应地更改我的答案…@Timorthy我正在使用httpClient模块。但我没有受到质疑。由于我们使用的是angular的最新版本,建议使用HttpClient模块。
dopost():SecondResponseType {
return this._http.get<InitialResponseType>('dammyUrl', {}, {
observe: 'response',
headers: {}
}).pipe(
switchMap((initialResponse: InitialResponseType) => this._http.post<SecondResponseType>('url', {}, {
observe: "response",
headers: {}
}))
});
}
dopost(): Observable<SecondResponseType> {
return this._http.get<SecondResponseType>('dammyUrl').pipe(
switchMap((initialResponse: InitialResponseType) => this._http.post<SecondResponseType>('url'))
});
}