簡體   English   中英

我應該如何在Angle2中使用多次進樣從另一個注射劑中擴展注射?

[英]How should I extend Injectable from another Injectable with many Injections in angular2?

可以這樣做嗎? (因為我試過,但沒有成功):

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

看到這個討論 ,為什么沒有辦法解決它。

這肯定會解決您的問題。

@Injectable()
class A {
  constructor(private http: Http){ // <-- Injection in root class
  }
  foo(http:Http){    //<------receive parameter as Http type 
     http.get()...   //<------this will work for sure.
  };
}

import {Http} from '@angular/http';
@Injectable()
class B extends A{
  constructor(private http:Http){}

  bar() {
    this.foo(this.http);   //<----- passing this.http as a parameter
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM