美文网首页
移动端开发:ionic如何与服务端进行数据交互

移动端开发:ionic如何与服务端进行数据交互

作者: 微笑涛声 | 来源:发表于2020-04-22 09:10 被阅读0次

一、查看数据库表内容(以新闻表为例)

image

二、使用postman检查服务端接口是否可以正常获取数据

1、getNewsList方法用于新闻表多个数据查询

image

2、getSingleNewsById方法用于新闻表单个个数据查询

image

三、编写移动端代码

1、建立模型类News

新建model包用于存放模型类,再新建News.ts文件,根据数据库的字段添加成员变量。
export class News{
    newsid:string;  //每条新闻对应的ID
    title1:string;  //新闻的标题1
    title2:string;  //新闻的标题2
    author:string;  //新闻的作者
    pbdate:number;  //发布新闻的时间
    content:string; //新闻的内容
}

2、建立service

(1)、新建config.service用来获取服务端主机头

  • 新建命令:ionic g service service/config
import { Injectable } from '@angular/core';
@Injectable({
  providedIn: 'root'
})
export class ConfigService {
  constructor() { }
    public HOST="https://blog.mzwhzy.com"; //服务端主机头
}

(2)、新建news.service用来获取服务端getNewsList方法和getSingleNewsById方法的请求路径

  • 新建命令:ionic g service service/news
  • app.module.tsimports里面加入,HttpClientModule
import { Injectable } from '@angular/core';
import {ConfigService} from './config.service';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class NewsService {
   //构造方法引入ConfigService和HttpClient
  constructor(private config:ConfigService,
              private http:HttpClient) { }
    
    private getNewsListUrl=this.config.HOST+"/public/getNewsList";
    getNewsList(){
        return this.http.get(this.getNewsListUrl).toPromise();
    }

    private getSingleNewsUrl=this.config.HOST+"/public/getSingleNewsById";
    getSingleNews(id:string){
        let parm={
            "newsid":id
        }
        return this.http.post(this.getSingleNewsUrl,parm).toPromise();
    }
}

3、在显示组件中绑定

(1)、逻辑部分(ts文件)

import { Component } from '@angular/core';
import {NewsService} from '../service/news.service';
import {News} from '../model/News';
import {Router} from '@angular/router';

@Component({
  selector: 'app-tab2',
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss']
})
export class Tab2Page {
    newslist:Array<News>
    constructor(private newsservice:NewsService
        ,private router:Router) {
        this.newslist=new Array();
    }
//在页面组件没有加载前调用
    ionViewWillEnter(){
        this.loadNewsList();
    }

    loadNewsList(){
        this.newsservice.getNewsList()
            .then((data:any)=>{
                this.newslist=new Array();
                this.newslist=data;
            })
    }

    //这个用来单个查询
    goTonewsDetail(id:string){
        this.router.navigate(['newsdetail',{"id":id}])
    }

}

(2)、html部分

<ion-content >
  <ion-list>
                             <!--点击新闻触发(click)事件,跳转到新闻详情页面-->
    <ion-item *ngFor="let n of newslist" (click)="goTonewsDetail(n.newsid)">
      <ion-label>{{n.title1}}</ion-label>
      <ion-text>{{n.pbdate |date:'yyyy-MM-dd'}}</ion-text>
    </ion-item>
  </ion-list>
</ion-content>

现在可以测试,新闻表已经可以显示在页面上了。

4、显示新闻详情页面

(1)、新建一个页面用来显示新闻详情页

新建页面命令:ionic g page page/newsdetail

(2)、新闻详情页逻辑部分代码

import { Component, OnInit } from '@angular/core';
import {News} from "../../model/News";
import {ActivatedRoute} from "@angular/router";
import {NewsService} from "../../service/news.service";

@Component({
  selector: 'app-newsdetail',
  templateUrl: './newsdetail.page.html',
  styleUrls: ['./newsdetail.page.scss'],
})
export class NewsdetailPage implements OnInit {

    id:string;
    news:News
    constructor(private parm:ActivatedRoute,
                private newsservice:NewsService) {
        this.news=new News();
        //接收URL参数
        this.id=this.parm.snapshot.paramMap.get("id");
    }

    ngOnInit() {
    }
    //当页面加载时运行
    ionViewWillEnter(){
        this.loadSingelNewsById();
    }
    
    //从服务器端获取单个新闻对象数据
    loadSingelNewsById(){
        this.newsservice.getSingleNews(this.id)
            .then((data:any)=>{
                this.news=new News();
                this.news=data;
            })
    }

}

(3)、新闻详情页html部分代码

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-back-button></ion-back-button>
    </ion-buttons>
    <ion-title>{{news.title2}}</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content>

  <ion-item >
    <ion-badge>作者:{{news.author}}</ion-badge>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <ion-badge>发布时间:{{news.pbdate |date:'yyyy-MM-dd'}}</ion-badge>
  </ion-item>
  {{news.content}}
</ion-content>

由于没有加入HTML管道,在显示新闻内容是会显示对应的html标记

四、效果演示

image

相关文章

  • 移动端开发:ionic如何与服务端进行数据交互

    一、查看数据库表内容(以新闻表为例) 二、使用postman检查服务端接口是否可以正常获取数据 1、getNews...

  • Android开发(19) 使用adb建立pc和android设

    需求背景 在我开发过的android应用中,大多都需要和 远程服务端 进行交互,需要从服务端获得数据或者上传数据。...

  • JSON和XML

    数据解析 在日常开发中,我们需要和服务端交互,传输数据的过程中就需要对服务端传输的数据进行解析 我们日常开发中最常...

  • iOS开发——解析XML数据

    移动端开发中,与后台服务数据请求打交道,现在通常是以JSON格式数据进行处理交互。最近碰到一个项目,后台数据交互返...

  • Python2.x接口测试—post请求

    Post请求是向服务器提交数据的一种请求,是与服务端进行交互的request,一般需要服务端对数据进行存储等处理。...

  • 2018-12-25

    Android网络请求库的对比 首先,Android端开发离不开与服务端的交互,与服务端交互就离不开网络请求;其次...

  • Gson 泛型封装model

    在项目开发中和服务端使用json数据格式进行交互的时候,通常服务端返回的数据会约定一个数据格式,例如: 我们真正需...

  • Java-Spring异常全局处理

    原由 在程序开发时,客户端与服务端进行交互的过程中,客户端需要对服务端返回的异常进行处理然后提示给用户该过程出了什...

  • ionic ,react-native , native 移动端

    移动端开发如何选型?这里介绍一下我眼中的ionic,react-native,native 三种移动端开发选型对比...

  • 关于Ionic、React-Native、Native的那些事

    Ionic、React-Native、native开发移动app那个好 ? 移动端开发如何选型?这里介绍一下我眼中...

网友评论

      本文标题:移动端开发:ionic如何与服务端进行数据交互

      本文链接:https://www.haomeiwen.com/subject/qudbihtx.html