Angularjs 在基类中使用依赖项,而不必从继承类传递此依赖项

Angularjs 在基类中使用依赖项,而不必从继承类传递此依赖项,angularjs,typescript,angular,Angularjs,Typescript,Angular,在学习Angular2的过程中,我遇到了以下情况: 1个基类,使用angular2/http依赖项 1继承扩展基类的类 如何将angular2/http依赖注入基类的构造函数中,而不必在实例化继承类时将http作为参数传递 我在继承类中不使用http,所以我不想在那里看到它 比如说 // base class class CRUDService { // http is used in this service, so I need to inject it here co

在学习Angular2的过程中,我遇到了以下情况:

  • 1个基类,使用angular2/http依赖项
  • 1继承扩展基类的类
如何将angular2/http依赖注入基类的构造函数中,而不必在实例化继承类时将http作为参数传递

我在继承类中不使用http,所以我不想在那里看到它

比如说

// base class
class CRUDService {

    // http is used in this service, so I need to inject it here
    constructor(@Inject(Http) http) {}

    findAll() {
        return this.http.get('http://some.api.com/api/some-model');
    }
}

// inheriting class
class ThingService extends CRUDService {

    constructor() {
        // because we extend the base class, we need to call super()
        // this will fail since it expects http as a parameter
        // BUT i don't use http in this class, I don't want to inject it here
        super();
    }
}

理想情况下,我只需在基类中创建一个新的http实例,并像这样使用
let http=new http(),但这显然不起作用。

这不受支持。如果你想注入一些东西,你必须在子类的构造函数中列出它,如果你想把它传递给超类,你可以使用
super(someDependency)
。没有出路。

这不是角度限制,而是类型化类中非常常见的语言限制。

您需要将其注入继承类,但如果有帮助,您可以从基类访问它,执行类似于
(this).http.get(…)
(您不需要通过
super()显式传递它)
。我正试图把我的头绕到这一点上。在这个时刻,我觉得这太荒谬了……:(我听了很多。这是没有办法的,这就是ts(和许多其他语言)中的构造函数的方式)如果子类根本没有构造函数,则使用超类的构造函数,但如果子类有构造函数,则需要重复所有参数并将其传递给
super()