NG4 - http-GET = GET Data Access service
=========================================
import { Component,Output,OnInit,Input, EventEmitter } from '@angular/core'
getProduct.component.ts
------------------------
@Component({
selector:'getHttp',
styleUrls: ['./getHttp.scss'],
templateUrl: './getHttp.html',
})
export class GetHttpComponent implements OnInit{
...
constructor(private productService: ProductService) { }
...
getProduct(id: number): void {
this.productService.getProduct(id)
.subscribe(
(product: IProduct) => this.onProductRetrieved(product),
// ---> Instead of this (data:any)=>{ if(data.success){ //true } else { //false }}
(error: any) => this.errorMessage= <any>error
);
}
getProduct.service.ts
----------------------
...
import { Http, Response} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
exportclassProductService{
privatebaseUrl= 'www.myWebService.com/api/products';
constructor(privatehttp: Http) { }
getProduct(id: number): Observable<IProduct> {
// ---> Instead of this Observable<any> using object
consturl= `${this.baseUrl}/${id}`;
return this.http.get(url)
// http.get method in ng2 / 4 same
.map(this.extractData)
.catch(this.handleError);
}
}
private extractData(res: Response) {
let body = res
return body || {} ;
}
private handleError (error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
=========================================
import { Component,Output,OnInit,Input, EventEmitter } from '@angular/core'
getProduct.component.ts
------------------------
@Component({
selector:'getHttp',
styleUrls: ['./getHttp.scss'],
templateUrl: './getHttp.html',
})
export class GetHttpComponent implements OnInit{
...
constructor(private productService: ProductService) { }
...
getProduct(id: number): void {
this.productService.getProduct(id)
.subscribe(
(product: IProduct) => this.onProductRetrieved(product),
// ---> Instead of this (data:any)=>{ if(data.success){ //true } else { //false }}
(error: any) => this.errorMessage= <any>error
);
}
getProduct.service.ts
----------------------
...
import { Http, Response} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
exportclassProductService{
privatebaseUrl= 'www.myWebService.com/api/products';
constructor(privatehttp: Http) { }
getProduct(id: number): Observable<IProduct> {
// ---> Instead of this Observable<any> using object
consturl= `${this.baseUrl}/${id}`;
return this.http.get(url)
// http.get method in ng2 / 4 same
.map(this.extractData)
.catch(this.handleError);
}
}
private extractData(res: Response) {
let body = res
return body || {} ;
}
private handleError (error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
No comments:
Post a Comment