【发布时间】:2018-06-03 18:11:32
【问题描述】:
我正在HttpInterceptor 函数中编写一些代码。函数 intercept 应该返回一个类型为 HttpEvent<any> 的 Observable。
对我来说棘手的部分是我需要在此函数中进行异步调用,并且只有在我进入异步代码的“成功”函数时才能返回 Observable<HttpEvent<any>>。
所以我的问题是,我如何返回一个真正包含另一个不同类型的 Observable 的正确类型的 Observable?
这是我的代码
@Injectable()
export class MyInterceptor implements HttpInterceptor {
constructor(private valueService: ValueService){}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const duplicate = req.clone({
params: req.params.set('newVal', this.valueService.getNewValue()) // this.valueService.getNewValue() returns an Observable<string> !!
});
return next.handle(duplicate);
}
}
【问题讨论】:
-
您可以使用
flatMap。检查此链接:reactivex.io/documentation/operators/flatmap.html
标签: angular rxjs observable angular-http-interceptors