有可能做这样的事情吗?(因为我试过了,但没有成功):
@Injectable()
class A {
constructor(private http: Http){ // <-- Injection in root class
}
foo(){
this.http.get()...
};
}
@Injectable()
class B extends A{
bar() {
this.foo();
}
}
有可能做这样的事情吗?(因为我试过了,但没有成功):
@Injectable()
class A {
constructor(private http: Http){ // <-- Injection in root class
}
foo(){
this.http.get()...
};
}
@Injectable()
class B extends A{
bar() {
this.foo();
}
}
有点 - 你必须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
}
}