import { Injectable } from '@angular/core'; import {Http, Headers, Response} from '@angular/http'; import 'rxjs/add/operator/toPromise'; @Injectable() export class HttpService { jsonheader: any; constructor(private http: Http) { this.jsonheader = new Headers({'Content-Type': 'application/json'}); } private handleError(error: any): Promise<any> { alert('An error occurred.\nError text: '+error['_body']); return Promise.reject(error.message || error); } getInstances(url: string): Promise<any[]> { return this.http.get(url,{headers: this.jsonheader}).toPromise() .then(resp =>{ }).catch(this.handleError); }
【个人笔记重点,不作为参考】主题:angular2.0的http包装方法,demo
最新推荐文章于 2023-09-28 11:37:41 发布