Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a component with dependency injection?

I have the following class ModuleWithHttp:

@Injectable()
export default class {
  constructor(private fetchApi: FetchApi) {}
}

and I want to use it as follows:

@Component({
  selector: 'main',
  providers: [FetchApi]
})
export default class extends ModuleWithHttp {
  onInit() {
    this.fetchApi.call();
  }
}

So by extending a super class that already injects a dependency I want to have an access to it in its children.

I tried many different ways, even having super-class as a component:

@Component({
  providers: [FetchApi]
})
export default class {
  constructor(private fetchApi: FetchApi) {}
}

But still, this.fetchApi is null, even in super-class.

like image 215
Kamil Lelonek Avatar asked Nov 28 '15 11:11

Kamil Lelonek


People also ask

Can I extend Angular component?

Typescript and Angular give you a way to handle this encapsulation. Inherited components! Using class inheritance in TypeScript, you can declare a base component that contains common UI functionality and use it to extend any standard component you'd like.

How do you inject dependency in a method?

How can we inject a Dependency into a class when it's different for each operation? By supplying it as a method parameter. In cases where a Dependency can vary with each method call, or the consumer of such a Dependency can vary on each call, you can supply a Dependency via a method parameter.

Which component can be injected as a dependency?

The "Application Module" can be injected as a dependency in AngularJS.


2 Answers

If you want to avoid this "boiler plate" code injecting services in child classes just for injection in parent classes' constructor and then effectively using that services in child classes through inheritance, you could do this:

edit: from Angular 5.0.0 ReflectiveInjector has been deprecated in favour of StaticInjector, below is updated code to reflect this change

Have a services map with deps,

export const services: {[key: string]: {provide: any, deps: any[], useClass?: any}} = {   'FetchApi': {     provide: FetchApi,     deps: []   } } 

Have an Injector holder,

import {Injector} from "@angular/core";  export class ServiceLocator {   static injector: Injector; } 

set it up in AppModule,

@NgModule(...) export class AppModule {   constructor() {     ServiceLocator.injector = Injector.create(       Object.keys(services).map(key => ({         provide: services[key].provide,         useClass: services[key].provide,         deps: services[key].deps       }))     );   } } 

use the injector in parent class,

export class ParentClass {    protected fetchApi: FetchApi;    constructor() {     this.fetchApi = ServiceLocator.injector.get(FetchApi);     ....   } } 

and extend parent class so you don't need to inject the FetchApi service.

export class ChildClass extends ParentClass {   constructor() {     super();     ...   }    onInit() {     this.fetchApi.call();   } } 
like image 187
Petr Marek Avatar answered Sep 22 '22 15:09

Petr Marek


You have to inject fetchAPI in the super class and pass it down to the child class

export default class extends ModuleWithHttp {

  constructor(fetchApi: FetchApi) {
     super(fetchApi);
  }   

  onInit() {
    this.fetchApi.call();
  }
}

This is a feature of how DI works in general. The super class will instantiate the child via inheritance, but you have to supply the required parameters to the child.

like image 45
TGH Avatar answered Sep 20 '22 15:09

TGH