Reputation: 1145
I'm having some difficulties trying to inject a service into another in Angular. I'm trying to create a service by extending another class with an existing constructor, as follows:
@Injectable()
export class AuthHttpService extends AuthHttp {
constructor(options: AuthConfig, http: Http, defOpts?: RequestOptions) {
super(options, http, defOpts);
}
}
My issue is: I need to inject another service into it and I can't seem to put it as argument in the constructor (can't put a required argument after an optional one and if I put it as optional with ? then it doesn't work).
I've tried with injector.get
and it works reasonably well but it injects a different instance of the service than the other components and is breaking my app (I rely on some service properties to show information).
Any solution? Thanks!
Upvotes: 0
Views: 42
Reputation: 60626
To inject another service, just add it to the constructor before the optional parameter. Then it will be fine.
@Injectable()
export class AuthHttpService extends AuthHttp {
constructor(options: AuthConfig, http: Http,
otherservice: OtherService,
defOpts?: RequestOptions) {
super(options, http, defOpts);
}
}
Upvotes: 1