How can I extend Injectable from another Injectable with many injections in angular2?

Is it possible to do something like this? (because I tried and failed):

@Injectable() class A { constructor(private http: Http){ // <-- Injection in root class } foo(){ this.http.get()... }; } @Injectable() class B extends A{ bar() { this.foo(); } } 
+5
source share
2 answers

View - you must make a call to super constructor of the base class. Just do the necessary dependencies:

 @Injectable() class A { constructor(private http: Http){ // <-- Injection in root class } foo(){ this.http.get()... }; } @Injectable() class B extends A{ constructor(http: Http) { super(http); } bar() { this.foo(); } } 

See this discussion on why there is no way around this.

+4
source

This will solve your problem for sure.

 @Injectable() class A { constructor(private http: Http){ // <-- Injection in root class } foo(http:Http){ //<------receive parameter as Http type http.get()... //<------this will work for sure. }; } 

 import {Http} from '@angular/http'; @Injectable() class B extends A{ constructor(private http:Http){} bar() { this.foo(this.http); //<----- passing this.http as a parameter } } 
0
source

All Articles