我应该如何在Angle2中使用多次进样从另一个注射剂中扩展注射?

kar*_*ina 6 dependency-injection inject injectable typescript angular

可以这样做吗?(因为我试过,但没有成功):

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


@Injectable()
class B extends A{
  bar() {
    this.foo();
  }
}
Run Code Online (Sandbox Code Playgroud)

rin*_*usu 6

有点 - 你必须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();
  }
}
Run Code Online (Sandbox Code Playgroud)

看到这个讨论,为什么没有办法解决它.