角扩展类构造函数重载

时间:2019-04-10 03:18:13

标签: angular constructor httpclient

我将以下类扩展到@ nebular / auth库中的NbLoginComponent。我需要向HttpClient添加构造函数以使用http。但是不能添加构造函数,因为NbLoginComponent默认构造函数期望4个我没有的参数。我添加了这些参数,但没有成功。有人可以告诉我另一种不使用构造函数初始化HttpClient的方法。

以下是我的代码

export class NgxLoginComponent extends NbLoginComponent {
  protected service: NbAuthService;
  protected options: {};
  protected cd: ChangeDetectorRef;
  protected router: Router;
  constructor(
    service: NbAuthService,
    options: {},
    cd: ChangeDetectorRef,
    router: Router,
    private http: HttpClient
  ) {
    super(service, options, cd, router);
  }
  }

NbLoginComponent的默认构造函数需要构造函数中的前四个参数。我需要另一种初始化HttpClient的方法。

2 个答案:

答案 0 :(得分:0)

我没有使用构造函数,而是使用了以下代码,它解决了我的问题。

http = new HttpClient(
    new HttpXhrBackend({ build: () => new XMLHttpRequest() })
  );

答案 1 :(得分:0)

您应该使用@Inject(NB_AUTH_OPTIONS)受保护的选项,而不要使用选项:{},例如:

export class NgxLoginComponent extends NbLoginComponent {
  protected service: NbAuthService;
  protected options: {};
  protected cd: ChangeDetectorRef;
  protected router: Router;
  constructor(
    service: NbAuthService,
    @Inject(NB_AUTH_OPTIONS) protected options,
    cd: ChangeDetectorRef,
    router: Router,
    private http: HttpClient
  ) {
    super(service, options, cd, router);
  }
  }