Angular 2 - 扩展类中的依赖注入

时间:2016-12-20 09:18:31

标签: angular dependency-injection angular-services

我创建了从XHRBackend扩展的类来捕获401错误全局。当发生401错误时,如何在此类中注入我的AuthService以注销用户?

export class CustomXHRBackend extends XHRBackend {
  public createConnection(request: Request): XHRConnection {
    let connection: XHRConnection = super.createConnection(request);
    connection.response = connection.response
      .catch(this.handleError);

    return connection;
  }

  private handleError(error: Response | any) {
    console.log(error['status']);
    if(error['status'] === 401) {
      console.log('Navigate to login page.');
    }

    return Observable.throw(error);
  }
}

0 个答案:

没有答案