gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:26:11 28 4
gpt4 key购买 nike

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

@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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com