ionic3+Angular4實現(xiàn)接口請求及本地json文件讀取

一 準(zhǔn)備工作

首先,ionic3+Angular4的開發(fā)環(huán)境你得有,這里就不贅述。環(huán)境準(zhǔn)備好,創(chuàng)建一個空白項目,模板自選。

二 實現(xiàn)過程

1 新建json文件和service

service記得在app.module.ts中引用

json和service

2 json文件格式

格式類似這樣,根據(jù)實際需求決定。

[
  {
    "id":"1",
    "name":"xiehan",
    "age":"24",
    "message":"測試json文件讀取"
  },
  {
    "id":"2",
    "name":"xiehan",
    "age":"24",
    "message":"測試json文件讀取"
  },
  {
    "id":"3",
    "name":"xiehan",
    "age":"24",
    "message":"測試json文件讀取"
  },
  {
    "id":"4",
    "name":"xiehan",
    "age":"24",
    "message":"測試json文件讀取"
  }
]

3 service

import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Http, Response} from '@angular/http';
import "rxjs/add/operator/map";


@Injectable()
export class DemoService {

  constructor(private httpService: Http){
  }
  // 網(wǎng)絡(luò)接口請求
  getHomeInfo(): Observable<Response> {
    return this.httpService.request('http://jsonplaceholder.typicode.com/users')
  }

  // 本地json文件請求
  getRequestContact(){
    return  this.httpService.get("assets/json/message.json")
  }
}

4 數(shù)據(jù)顯示

1 網(wǎng)絡(luò)接口請求

//home.ts
import {ChangeDetectorRef, Component} from '@angular/core';
import { NavController } from 'ionic-angular';
import {DemoService} from "../../services/demo.service";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  // 接收數(shù)據(jù)用
  listData: Object;
  // 依賴注入
  constructor(public navCtrl: NavController,
              private ref: ChangeDetectorRef,
              private demoService: DemoService,) {
  }

  ionViewDidLoad() {
    // 網(wǎng)絡(luò)請求
    this.getHomeInfo();
  }

  getHomeInfo(){
    this.demoService.getHomeInfo()
      .subscribe(res => {
        this.listData = res.json();
        // 數(shù)據(jù)格式請看log
        console.log("listData------->",this.listData);
        this.ref.detectChanges();
      }, error => {
        console.log(error);
      });
  }
}

//home.html
<ion-header>
  <ion-navbar>
    <ion-title>首頁</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <ion-list *ngFor="let item of listData">
    <ion-item>
    <!--?是Angular特定語法,相當(dāng)于判斷數(shù)據(jù)是否存在,有則顯示無則不顯示-->
      {{item?.name}}
    </ion-item>
  </ion-list>
</ion-content>

效果圖

效果圖1.png

2 本地json文件請求

service中已經(jīng)寫了getRequestContact()方法對本地json文件讀取。

//contact.ts
import {ChangeDetectorRef, Component} from '@angular/core';
import { NavController } from 'ionic-angular';
import {DemoService} from "../../services/demo.service";

@Component({
  selector: 'page-contact',
  templateUrl: 'contact.html'
})
export class ContactPage {

  contactInfo=[];

  constructor(public navCtrl: NavController,
              private demoService: DemoService,
              private ref: ChangeDetectorRef,) {

  }

  ionViewDidLoad() {
    // 網(wǎng)絡(luò)請求
    this.getRequestContact();
  }

  getRequestContact(){
    this.demoService.getRequestContact()
      .subscribe(res => {
        this.contactInfo = res.json();
        console.log("contactInfo------->",this.contactInfo);
        this.ref.detectChanges();
      }, error => {
        console.log(error);
      });
  }
}

// contact.html
<ion-header>
  <ion-navbar>
    <ion-title>
      聯(lián)系人
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <ion-list>
    <ion-item *ngFor="let item of contactInfo">
      <div style="display: flex;flex-direction: column;">
        <span>姓名:{{item?.name}}</span>
        <span>年齡:{{item?.age}}</span>
        <span>信息:{{item?.message}}</span>
      </div>
    </ion-item>
  </ion-list>
</ion-content>

效果圖

效果圖2.png

補(bǔ)充

angularjs1實現(xiàn)方式

getMeetingDetailInfo($http,$scope);

    function getMeetingDetailInfo($http,$scope) {
      console.log("請求json-------->");
      $http.get('file/meeting.json').success(function(data) {
        $scope.data = data;
        console.log("data----->",data);
      });
    }

三 總結(jié)

1.所有創(chuàng)建的page要在app.module.ts中引用;
2.service要在app.module.ts中引用;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。