我应该如何从另一个 Injectable 扩展 Injectable,并在 angular2 中进行多次注射?
How should I extend Injectable from another Injectable with many Injections in angular2?
有没有可能做这样的事情? (因为我尝试过,但没有成功):
@Injectable()
class A {
constructor(private http: Http){ // <-- Injection in root class
}
foo(){
this.http.get()...
};
}
@Injectable()
class B extends A{
bar() {
this.foo();
}
}
有点 - 您必须 super
调用基础 class 的构造函数。只需传递所需的依赖项:
@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, why there is no way around it.
这肯定能解决您的问题。
@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
}
}
有没有可能做这样的事情? (因为我尝试过,但没有成功):
@Injectable()
class A {
constructor(private http: Http){ // <-- Injection in root class
}
foo(){
this.http.get()...
};
}
@Injectable()
class B extends A{
bar() {
this.foo();
}
}
有点 - 您必须 super
调用基础 class 的构造函数。只需传递所需的依赖项:
@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, why there is no way around it.
这肯定能解决您的问题。
@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
}
}