2016-09-12 15 views
5

क्या ऐसा कुछ करना संभव है? (मैंने कोशिश की कारण, और सफल नहीं किया है):कोणीय 2 में कई इंजेक्शन के साथ मैं इंजेक्शन योग्य इंजेक्शन से इंजेक्शन कैसे बढ़ा सकता हूं?

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

 

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

उत्तर

4

का प्रकार - आप अपने आधार वर्ग के निर्माता के लिए एक 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(); 
    } 
} 

this discussion देखें, क्यों इसके चारों ओर कोई रास्ता नहीं है।

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 
    } 
} 
संबंधित मुद्दे