我应该如何从angular2中的多次注入中将另一个Injectable扩展为Injectable? [英] How should I extend Injectable from another Injectable with many Injections in angular2?

查看:91
本文介绍了我应该如何从angular2中的多次注入中将另一个Injectable扩展为Injectable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以做这样的事情? (因为我尝试过,但没有成功):

Is it possible to do something like this? (cause I tried, and haven't succeed):

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


@Injectable()
class B extends A{
  bar() {
    this.foo();
  }
}

推荐答案

种类-您必须对基类的构造函数进行super调用.只需传递所需的依赖项即可:

Kind of - you have to make a super call to the constructor of your base class. Just pass down the needed 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, why there is no way around it.

这篇关于我应该如何从angular2中的多次注入中将另一个Injectable扩展为Injectable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆