修补运算符到可点运算符,角度为5到6

时间:2018-08-13 18:51:16

标签: angular rxjs angular-http-interceptors rxjs6 rxjs-pipeable-operators

我正从5号角迁移到6号角,我还不太清楚新的管道运算符语法。

考虑我的角度5 http拦截器

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.request = req;

//do stuff
return next.handle(this.request)
  .do(event => {

   //do logging stuff
  },
  err => {
    //do error stuff
  });
}

这是我尝试将其转换为pipe方式

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.request = req;

//do stuff
return next.handle(this.request).pipe(
  tap(event => {

   //do logging stuff
  },
  err => {
    //do error stuff
  }));
}

但是,这不能编译-使用pipe函数的整个过程都带有以下错误。

Severity    Code    Description Project File    Line    Suppression State
Error   TS2345  (TS) Argument of type 'MonoTypeOperatorFunction<{}>' is not 
assignable to parameter of type 'UnaryFunction<Observable<HttpEvent<any>>, 
Observable<any>>'.
Types of parameters 'source' and 'source' are incompatible.
Type 'Observable<HttpEvent<any>>' is not assignable to type 
'Observable<{}>'.
  Property 'source' is protected in type 'Observable<HttpEvent<any>>' but public in type 'Observable<{}>'.  C:\Users\stas.levich\Documents\Projects\EEA.Projects\DEP\MassAir\Web (tsconfig or jsconfig project) C:\Users\stas.levich\Documents\Projects\EEA.Projects\DEP\MassAir\Web\src\app\services\httpinterceptor.service.ts    44  Active

2 个答案:

答案 0 :(得分:0)

<mappers> <mapper resource="mappers/account.xml" /> </mappers> 需要一组运算符。 pipe是运算符。 tap函数不是是有效的RxJs运算符。尝试err =>

这是我的一种get方法的示例:

catchError

答案 1 :(得分:0)

尝试以下代码:

return next.handle(authReq).pipe(
        tap(event => {
            return event;
        }),
        catchError(error => {

            // On Error
        }),
        finalize(() => {
         // on finalize
        })
    );