首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Angular请求: HttpRequest<any>不是通用

Angular请求: HttpRequest<any>不是通用
EN

Stack Overflow用户
提问于 2019-07-25 03:06:24
回答 1查看 159关注 0票数 0

我正在创建一个服务“错误拦截器”,但当我尝试实例化一个HttpRequest时,它不允许。

我是Angular和Web App的新手。

这是我的尝试

代码语言:javascript
运行
复制
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http';
import { HttpRequest } from 'selenium-webdriver/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req).pipe(catchError(error => {
            if (error instanceof HttpErrorResponse) {
                const applicationerror = error.headers.get('Application-Error');
                if (applicationerror) {
                    console.log(applicationerror);
                    return throwError(applicationerror);
                }
            }
        })
        );
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-25 03:12:35

这是因为您要从selenium-webdriver/http而不是从@angular/common/http导入HttpRequest

删除此命令:

代码语言:javascript
运行
复制
import { HttpRequest } from 'selenium-webdriver/http';

然后像这样添加HttpRequest

代码语言:javascript
运行
复制
import { HttpInterceptor, HttpHandler, HttpEvent, HttpErrorResponse, HttpRequest } from '@angular/common/http';
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57189684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档