2016-06-20 12 views
5

मैं कुछ सेवाओं, जो एक ही कोड के अंदर होता है:टाइपस्क्रिप्ट में कक्षा का विस्तार कैसे करें?

constructor (private http: Http) { 
    //use XHR object 
    let _build = (<any> http)._backend._browserXHR.build; 
    (<any> http)._backend._browserXHR.build =() => { 
     let _xhr = _build(); 
     _xhr.withCredentials = true; 
     return _xhr; 
    }; 
} 

मैं अलग फाइल करने के लिए कोड http_base_clas.ts स्थानांतरित करने के लिए और possible.Here जितना कोड पुन: उपयोग करना चाहता था http_base_clas.ts है:

import {Http} from '@angular/http'; 

export class HttpBaseClass { 
    constructor (http: Http) { 
     //use XHR object 
     let _build = (<any> http)._backend._browserXHR.build; 
     (<any> http)._backend._browserXHR.build =() => { 
      let _xhr = _build(); 
      _xhr.withCredentials = true; 
      return _xhr; 
     }; 
    } 
} 

http_base_class को auth_service.ts में कैसे विस्तारित करें?

import {Injectable} from '@angular/core'; 
import {Http, Response} from '@angular/http'; 
import {Headers, RequestOptions} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 

@Injectable() 

export class AuthenticationService { 
result: { 
    ok: boolean; 
}; 
private verifyUrl = 'http:example.com; 

constructor (private http: Http) { 

} 
private authRequest (url) { 
    let body = JSON.stringify({}); 
    let headers = new Headers({ 'Content-Type': 'application/json'}); 
    let options = new RequestOptions({ 
     headers: headers 
    }); 
    return this.http.post(url, body, options) 
     .map((res: Response) => res.json()) 
     .map(res => { 
      this.result = res; 
      return this.result.ok; 
     }); 
    } 
} 

उत्तर

6
class AuthenticationService extends HttpBaseClass { 
    constructor(http:Http) { 
    super(http); 
    } 

    private authRequest (url) { 
    ... 
    } 
} 
+0

मदद के लिए धन्यवाद। – Serhiy

+1

आपका नाम हर कोणीय प्रश्न पर है और आपका उत्तर कभी निराश नहीं होता है। धन्यवाद! –

+1

@ हर्बीशतिनी धन्यवाद। मेरे उत्तरों को सुनकर खुशी हुई :) –

संबंधित मुद्दे