Angular 11 通用类型 'HttpRequest<T>' 需要 1 个类型参数.ts(2314)

Hir*_*ndo 1 typescript angular angular11

我正在练习 Angular JWT 身份验证和授权。这是我的auth.interceptor.ts文件:

import { HTTP_INTERCEPTORS, HttpEvent } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { HttpInterceptor, HttpHandler, HttpRequest } from "@angular/common/http";
import { Observable } from "rxjs";

import { TokenStorageService } from "../services/token-storage.service";


const TOKEN_HEADER_KEY = "Authorization";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
    
    constructor(private token:TokenStorageService) { }

    intercept(req: HttpRequest, next:HttpHandler): Observable<any> {
        let authReq = req;
        const token = this.token.getToken();
        if (token != null) {
            authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer' + token) });
        }
        return next.handle(authReq);
    }
}

export const authInterceptorproviders = [
    { provide: HTTP_INTERCEPTORS, userClass: AuthInterceptor, multi: true }
];
Run Code Online (Sandbox Code Playgroud)

HttpRequest我从(第 10 行)收到此错误

Generic type 'HttpRequest<T>' requires 1 type argument(s).ts(2314)
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此错误?有什么解决方案吗?

err*_*rau 6

“HttpRequest”中的您应该是具体类型复杂或原始

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      /// your code
    }
Run Code Online (Sandbox Code Playgroud)