国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

ionic3+Angular4實現接口請求及本地json文件讀取示例

2019-11-19 15:11:30
字體:
來源:轉載
供稿:網友

一 準備工作

首先,ionic3+Angular4的開發環境你得有,這里就不贅述。環境準備好,創建一個空白項目,模板自選。

二 實現過程

1 新建json文件和service

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

json和service

2 json文件格式

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

[ {  "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){ } // 網絡接口請求 getHomeInfo(): Observable<Response> {  return this.httpService.request('http://jsonplaceholder.typicode.com/users') } // 本地json文件請求 getRequestContact(){  return this.httpService.get("assets/json/message.json") }}

4 數據顯示

1 網絡接口請求

//home.tsimport {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 { // 接收數據用 listData: Object; // 依賴注入 constructor(public navCtrl: NavController,       private ref: ChangeDetectorRef,       private demoService: DemoService,) { } ionViewDidLoad() {  // 網絡請求  this.getHomeInfo(); } getHomeInfo(){  this.demoService.getHomeInfo()   .subscribe(res => {    this.listData = res.json();    // 數據格式請看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特定語法,相當于判斷數據是否存在,有則顯示無則不顯示-->   {{item?.name}}  </ion-item> </ion-list></ion-content>

效果圖


2 本地json文件請求

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

//contact.tsimport {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() {  // 網絡請求  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>   聯系人  </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>

效果圖


三 總結

1.所有創建的page要在app.module.ts中引用;
2.service要在app.module.ts中引用;

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗江县| 阳春市| 鄱阳县| 陆良县| 桃园县| 乡城县| 高密市| 香港 | 长岛县| 兴安县| 平果县| 景德镇市| 札达县| 仙桃市| 澄江县| 抚顺县| 彩票| 新丰县| 邯郸县| 休宁县| 登封市| 南溪县| 井研县| 伊春市| 临朐县| 盐城市| 泰顺县| 军事| 沈丘县| 咸宁市| 扶余县| 砚山县| 青田县| 和田市| 长兴县| 铅山县| 曲沃县| 正蓝旗| 图木舒克市| 巴中市| 咸宁市|