Как мне расширить Injectable от другого Injectable со многими Injectionable в angular2?
Можно ли сделать что-то подобное? (потому что я пытался, но не удалось):
@Injectable()
class A {
constructor(private http: Http){ // <-- Injection in root class
}
foo(){
this.http.get()...
};
}
@Injectable()
class B extends A{
bar() {
this.foo();
}
}
2 ответа
Решение
Вид - вы должны сделать super
вызовите конструктор вашего базового класса. Просто укажите необходимые зависимости:
@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();
}
}
Посмотрите это обсуждение, почему нет никакого способа обойти это.
Это точно решит вашу проблему.
@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
}
}