ionic3+Angular4實(shí)現(xiàn)接口請(qǐng)求及本地json文件讀取示例

一 準(zhǔn)備工作

專注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)天壇街道免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

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

二 實(shí)現(xiàn)過程

1 新建json文件和service

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

ionic3+Angular4實(shí)現(xiàn)接口請(qǐng)求及本地json文件讀取示例

json和service

2 json文件格式

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

[
 {
  "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ò)接口請(qǐng)求
 getHomeInfo(): Observable<Response> {
  return this.httpService.request('http://jsonplaceholder.typicode.com/users')
 }

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

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

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

//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ò)請(qǐng)求
  this.getHomeInfo();
 }

 getHomeInfo(){
  this.demoService.getHomeInfo()
   .subscribe(res => {
    this.listData = res.json();
    // 數(shù)據(jù)格式請(qǐng)看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>

效果圖

ionic3+Angular4實(shí)現(xiàn)接口請(qǐng)求及本地json文件讀取示例

2 本地json文件請(qǐng)求

service中已經(jīng)寫了getRequestContact()方法對(duì)本地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ò)請(qǐng)求
  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 >
    <span>姓名:{{item?.name}}</span>
    <span>年齡:{{item?.age}}</span>
    <span>信息:{{item?.message}}</span>
   </div>
  </ion-item>
 </ion-list>
</ion-content>

效果圖

ionic3+Angular4實(shí)現(xiàn)接口請(qǐng)求及本地json文件讀取示例

三 總結(jié)

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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁名稱:ionic3+Angular4實(shí)現(xiàn)接口請(qǐng)求及本地json文件讀取示例
本文來源:http://muchs.cn/article26/jejpcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、品牌網(wǎng)站制作、企業(yè)建站、網(wǎng)站設(shè)計(jì)公司、用戶體驗(yàn)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)