K Rajitha
K Rajitha

Reputation: 316

Angular extended class constructor overloading

I have following class extended to NbLoginComponent in @nebular/auth library. I need to add a constructor to HttpClient to use http. But cannot add a constructor because the NbLoginComponent default constructor expecting 4 parameters that i haven't. I have added those parameters but no success. Can some one tell me another way to initialize HttpClient without using constructor.

Following is my code

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);
  }
  }

The first four arguments in the constructor are required by the default constructor of NbLoginComponent. I need another way to initialize HttpClient.

Upvotes: 1

Views: 572

Answers (2)

Pedro Silva
Pedro Silva

Reputation: 21

You should use @Inject(NB_AUTH_OPTIONS) protected options, instead of options: {}, for example:

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);
  }
  }

Upvotes: 2

K Rajitha
K Rajitha

Reputation: 316

Rather than using the constructor i have used following and it resolve my issue.

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

Upvotes: 0

Related Questions