angular - 我应该如何从另一个 Injectable 扩展 Injectable,并在 angular2 中进行多次注入(inject)?

标签 angular typescript dependency-injection inject injectable

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

@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();
  }
}

See this discussion, why there is no way around it.

关于angular - 我应该如何从另一个 Injectable 扩展 Injectable,并在 angular2 中进行多次注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39452578/

相关文章:

android - 注入(inject) Otto 事件总线而不是使用静态单例的优势

javascript - 社交媒体视频列表中的视频播放器损坏

angular - 如何从componentPortal实例中关闭 Material 覆盖

css - 在组件级别更改 primeng CSS

angular - 获取组件中的路由参数

javascript - 如何从全局函数访问 Angular 分量函数 - 请兼容 IE11

c# - 如何使用 Unity 2.0 注入(inject) Log4Net ILog 实现

UrlMatcher 中的 Angular 6+ 依赖注入(inject)和异步

angular - 以 Angular 4 的路线更改自动保存表单/模型内容

TypeScript:联合类型上的对象可能为空