Angular Http Interceptor - fromPromise error - TypeError: (0 , x.fromPromise) is not a function

Angular Http Interceptor - fromPromise error - TypeError: (0 , x.fromPromise) is not a function

我正在使用下面的函数将 recaptcha v3 附加到 Http 请求。

@Injectable()
export class RecaptchaInterceptor implements HttpInterceptor {
constructor(private recaptchaService: ReCaptchaService) { }

intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {    
  return fromPromise(this.recaptchaService.execute({ action: 'login' }))
    .pipe(switchMap(token => {
      const headers = httpRequest.headers
        .set('recaptcha', token)
      const reqClone = httpRequest.clone({
        headers
      });
      return next.handle(reqClone);
    }));
}

在本地主机上工作正常,但由于某些原因,发布后出现以下错误:

Uncaught (in promise): TypeError: (0 , x.fromPromise) is not a function TypeError: (0 , x.fromPromise) is not a function

接下来我可以尝试什么?

您可以使用 from 而不是 fromPromise,因为我认为您使用的是 rxjs 6+

import { from } from 'rxjs';

...
return from(this.recaptchaService.execute({ action: 'login' }))