角5:通用类型'HttpRequest <t>'

时间:2018-07-17 07:49:10

标签: angular http angular-http angular-httpclient

写拦截器后,我得到一个错误:

error TS2314: Generic type 'HttpRequest<T>' requires 1 type argument(s).

这是我的源代码:

import { Injectable } from '@angular/core';
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { UserService } from '@dockers/core/user/user.service';

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
    constructor(private userService: UserService) {}

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req).catch((err: any) => {
            console.log('Caught error', err);
            if (err instanceof HttpErrorResponse && err.status === 401) {
                this.userService.logoutUser();
            }
            return Observable.throw(err);
        });
    }
}

任何解决方案和帮助都将非常有用。谢谢!

1 个答案:

答案 0 :(得分:0)

您的问题的答案是<T>中的'HttpRequest<T>'应该是具体的复杂类型还是原始类型