10

Is it possible to do something like this? (cause I tried, and haven't succeed):

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


@Injectable()
class B extends A{
  bar() {
    this.foo();
  }
}
karina
  • 789
  • 9
  • 26

2 Answers2

11

Kind of - you have to make a super call to the constructor of your base class. Just pass down the needed dependencies:

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

Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
-1

This will solve your problem for sure.

@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
  }
}
micronyks
  • 54,797
  • 15
  • 112
  • 146