【问题标题】:Using Service in HttpInterceptor - Angular2在 HttpInterceptor 中使用服务 - Angular2
【发布时间】:2017-08-15 23:45:08
【问题描述】:

我有一个这样开头的 Httpinterceptor:

@Injectable()
export class InterceptedHttp extends Http {

  constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
    super(backend, defaultOptions);
  }

我想使用我在许多其他组件中创建和使用的服务。如果我像通常那样将我的服务添加到构造函数中,我会得到一个异常。显然这是因为构造函数不能与扩展类不同:

错误:提供的参数与调用目标的任何签名都不匹配。

是否仍然可以在我的 InterceptedHttp 类中使用该服务?

【问题讨论】:

    标签: angular service angular-http-interceptors


    【解决方案1】:

    不再推荐扩展Http。如果你使用 Angular 4.3+,你应该实现HttpInterceptor

    import {Injectable} from '@angular/core';
    import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
    
    @Injectable()
    export class NoopInterceptor implements HttpInterceptor {
      intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req);
      }
    }
    

    https://angular.io/guide/http#intercepting-all-requests-or-responses

    【讨论】:

    • 我不是——那又怎样?
    • @ill 在这一点上,你没有理由不更新并使用最新的。您可以查看此站点以了解版本之间的细微更改。 angular-update-guide.firebaseapp.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多