Ionic2實(shí)戰(zhàn)-Http請求模塊設(shè)計(jì)

前言

對于大部分App來說,都有大量前后端交互的需求,而Ionic2基于AngularJS2也提供了完善的Http通信模塊,可以讓我們方便地進(jìn)行前后臺的通信。

步驟

Http通信作為業(yè)務(wù)模塊都寫在了service文件中,以下步驟都是針對service的操作。

1、導(dǎo)入相關(guān)包

import { Http, Headers, RequestOptions } from '@angular/http';

2、初始化header消息頭

  headers: Headers;
  requestOptions: RequestOptions;

  constructor(
    private events: Events,
    private http: Http,
    private helper: Helper
  ) {
    this.headers = new Headers({'X-Requested-With': 'XMLHttpRequest'});
    this.requestOptions = new RequestOptions({headers: this.headers, withCredentials: true});
  }

3、準(zhǔn)備Http請求發(fā)送函數(shù)

  loadProductPriceList(params): Promise<CoalPrice> {
    let api: string = this.helper.getAPP('product/getProductPriceList');
    let data: Object = params;

    return this.http.post(api, data, this.requestOptions)
    .toPromise()
      .then(response => response.json())
      .catch(this.handleError);
  }

  // handle error
  private handleError(error: any) {
    return Promise.reject(error.message || error);
  }

4、調(diào)用發(fā)送函數(shù)

  loadProductPriceList(params) {

    this.coalService.loadProductPriceList(params)
    .then(ret => {
      this.productPriceList = ret;
    }, (data) => {

    });
  }

完整代碼附錄

import { Injectable } from '@angular/core';
import { Events } from 'ionic-angular';
import { Http, Headers, RequestOptions } from '@angular/http';

import { CoalPrice } from '../models/coalPrice.model';
import { Helper } from '../../common/services/helper.service';


@Injectable()
export class CoalService {
  headers: Headers;
  coalPrices: CoalPrice[] = [];
  requestOptions: RequestOptions;

  userUpdateAvatarAPI: string = this.helper.getAPI('user/update-avatar');


  //
  // constructor
  constructor(
    private events: Events,
    private http: Http,
    private helper: Helper
  ) {
    this.headers = new Headers({'X-Requested-With': 'XMLHttpRequest'});
    this.requestOptions = new RequestOptions({headers: this.headers, withCredentials: true});
  }


  getProductTypeList(data) {
    let api: string = this.helper.getAPP('product/getProductTypeList');

    return this.http.post(api, data, this.requestOptions)
      .toPromise()
      .then((response) => {
        return response.json();
      })
      .catch(this.handleError);
  }

  //
  loadProductPriceList(params): Promise<CoalPrice> {
    let api: string = this.helper.getAPP('product/getProductPriceList');
    let data: Object = params;

    return this.http.post(api, data, this.requestOptions)
    .toPromise()
      .then(response => response.json())
      .catch(this.handleError);
  }

  //
  // handle error
  private handleError(error: any) {
    return Promise.reject(error.message || error);
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,628評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,160評論 25 708
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,740評論 18 399
  • 這幅作品已經(jīng)歷時(shí)6年多了,當(dāng)時(shí)開始學(xué)習(xí)手縫高等課程,中島凱西的書出版,就看中了這個(gè)作品。因?yàn)槟軌虬迅叩茸鳂I(yè)中的婚戒...
    百合手工閱讀 389評論 0 4
  • 前不久和美國同事吃飯,我負(fù)責(zé)點(diǎn)餐,犯了一個(gè)錯誤。點(diǎn)了雞爪,還有鴨舌。 經(jīng)好心同事提醒說,有老外在,不應(yīng)該點(diǎn):爪類,...
    明小羽閱讀 1,358評論 0 0

友情鏈接更多精彩內(nèi)容