8

このようなことは可能ですか?(私が試したが成功しなかったため):

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


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

4

2 に答える 2

7

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

このディスカッションを参照してください。なぜそれを回避する方法がないのですか。

于 2016-09-12T14:35:04.333 に答える
0

これで問題は確実に解決します。

@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
  }
}
于 2016-09-12T15:10:46.610 に答える