这个错误通常是由于使用过时的 RxJS 版本导致的,特别是 RxJS5 和 RxJS6 中的更改。在最新的版本中,fromPromise 已经被重命名为 from,所以需要使用 from 代替 fromPromise。
下面给出一个示例代码:
import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable, from } from 'rxjs'; import { switchMap } from 'rxjs/operators';
@Injectable() export class AuthInterceptor implements HttpInterceptor {
constructor() {}
intercept(req: HttpRequest
return from(getToken()).pipe(
switchMap(token => {
// Clone the request to add the new header
const authReq = req.clone({ headers: req.headers.set('Authorization', 'Bearer ' + token) });
// Pass on the cloned request instead of the original request.
return next.handle(authReq);
})
);
} }
function getToken(): Promise