麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > JavaScript > 正文

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

2019-11-19 15:11:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一 準(zhǔn)備工作

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

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

1 新建json文件和service

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

json和service

2 json文件格式

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

[ {  "id":"1",  "name":"xiehan",  "age":"24",  "message":"測(cè)試json文件讀取" }, {  "id":"2",  "name":"xiehan",  "age":"24",  "message":"測(cè)試json文件讀取" }, {  "id":"3",  "name":"xiehan",  "age":"24",  "message":"測(cè)試json文件讀取" }, {  "id":"4",  "name":"xiehan",  "age":"24",  "message":"測(cè)試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.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 { // 接收數(shù)據(jù)用 listData: Object; // 依賴(lài)注入 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>首頁(yè)</ion-title> </ion-navbar></ion-header><ion-content padding> <ion-list *ngFor="let item of listData">  <ion-item>  <!--?是Angular特定語(yǔ)法,相當(dāng)于判斷數(shù)據(jù)是否存在,有則顯示無(wú)則不顯示-->   {{item?.name}}  </ion-item> </ion-list></ion-content>

效果圖


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

service中已經(jīng)寫(xiě)了getRequestContact()方法對(duì)本地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() {  // 網(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 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>

效果圖


三 總結(jié)

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美特一级片 | 日韩精品一二三区 | 九草av| 在线看日本 | 麻豆小视频在线观看 | 成人黄色短视频在线观看 | 91免费视频版 | 超碰97在线人人 | 羞羞答答www网站进入 | 美国一级毛片片aa久久综合 | 国产精品18久久久久久久久 | 蜜桃av鲁一鲁一鲁一鲁 | 羞羞的视频免费在线观看 | 护士hd欧美free性xxxx | 中文欧美日韩 | 久久精品电影网 | 久久精品欧美一区 | 久久精品99国产国产精 | 国内自拍网址 | 日本在线视频免费 | 欧美黄色片一级 | 日本在线观看一区二区 | 久久精品79国产精品 | 主播粉嫩国产在线精品 | 国产精品午夜在线观看 | 一区二区三区欧美日韩 | 成人免费观看毛片 | 成人午夜一区二区 | 九九热视频这里只有精品 | 午夜亚洲影院 | 日韩毛片毛片久久精品 | 日韩在线激情 | 亚洲精品久久久久久下一站 | 精品久久久久久久久久久久 | 中文字幕免费在线看 | wwwxxx国产 | 亚洲第九十九页 | 免费特黄 | 嗯哈~不行好大h双性 | 黄色伊人网站 | 国产精品18久久久久久久 |