5
Tôi có vài dịch vụ, trong đó có cùng mã bên trong:Làm thế nào để mở rộng một lớp trong TypeScript?
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;
};
}
tôi muốn di chuyển mã tập tin riêng biệt http_base_clas.ts và sử dụng lại mã càng nhiều càng possible.Here là 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;
};
}
}
Làm cách nào để mở rộng http_base_class thành 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;
});
}
}
cảm ơn bạn đã trợ giúp. – Serhiy
Tên bạn là trên mọi câu hỏi góc cạnh và câu trả lời của bạn không bao giờ thất vọng. Cảm ơn bạn! –
@HerbiShtini cảm ơn. Vui mừng khi nghe câu trả lời của tôi rất hữu ích :) –