本文转自:https://blog.csdn.net/lyt_angularjs/article/details/81145468

版权声明:本文为博主原创文章,转载请注明出处。谢谢! https://blog.csdn.net/lyt_angularjs/article/details/81145468
前言:
- 从别人的博客中获取知识,将知识写成博客留与后来人。
- 写博客既是对知识的总结梳理,也方便以后需要用的时候随时来查看。

一、创建provider
1.ionic g provider storage 创建了一个名为storage的服务在src/providers里;会自动添加到app.module.ts中。
2.先来丰富一下这个服务吧

//storage.ts
import { Injectable } from '@angular/core';

@Injectable()
export class StorageProvider {

constructor() {
console.log('Hello StorageProvider Provider');
}
public setItem(key,value){
localStorage.setItem(key,JSON.stringify(value))
}
public getItem(key){
return JSON.parse(localStorage.getItem(key))
}
public removeItem(key){
localStorage.removeItem(key)
}
}

3.接下来开始使用了

//home.html
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>

<ion-content padding>
<h2>{{msg}}</h2>
<button ion-button class="btn-login" color="secondary" *ngIf="!isLogin" (click)="goLoginPage()">去登录页面</button>
<button ion-button class="btn-login" *ngIf="isLogin" (click)="logout()">退出登录</button>
</ion-content>

//html.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LoginPage } from "../login/login";//引入页面
import { StorageProvider } from "../../providers/storage/storage";//引入服务

@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
msg:string;//提示信息
isLogin:boolean=false;//是否登录
constructor(
public navCtrl: NavController,
public storage:StorageProvider,
) {
this.init();
}
init(){
//判断是否登录
this.isLogin=this.storage.getItem('isLogin');
let account=this.storage.getItem('account');
if(account){
this.msg=`欢迎你,${account}`;
}else{
this.msg=`还没登录呢,大兄弟。`;
}
}
goLoginPage(){
this.navCtrl.push(LoginPage);//路由跳转-前进
}
logout(){
//删除localStorage里面的登录信息
this.storage.removeItem('isLogin');
this.storage.removeItem('account');
this.init();
}
}

//login.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { TabsPage } from "../tabs/tabs";
import { StorageProvider } from "../../providers/storage/storage"

//定义接口格式
interface User {
account: string;
password:string;
}

@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
user:User={
account:'admin',
password:undefined,
}
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public storage:StorageProvider
) {
}
login(){
//设置登录状态为已登录
this.storage.setItem('isLogin',true);
this.storage.setItem('account',this.user.account);
//设置某个页面为app的路由根页面,从此以后不能再后退页面
//左上角的<返回没有了,在手机上后退也不行了
this.navCtrl.setRoot(TabsPage);
}
goBack(){
this.navCtrl.pop();//路由跳转-后退
}
}

结果如图:

4.点击登录,结果如图:

5.点击退出登录,又回到第一张图片了

二、http请求、图文列表
ionic g provider api 在src/providers/api中
在api.ts中写get和post请求
import { Http,Headers,Response } from '@angular/http';
import { Injectable } from '@angular/core';

@Injectable()
export class ApiProvider {
//定义post请求需要的头部
public headers=new Headers({'Content-Type':'application/json'});
constructor(public http: Http) {
console.log('Hello ApiProvider Provider');
}
//实例get请求
public getList(){
return new Promise((resolve, reject) => {
this.http.get('https://jsonplaceholder.typicode.com/albums/1/photos')
.subscribe((res:Response)=>{
resolve(res.json())
},err=>{
console.dir(err)
reject()
});
});
}
//实例post请求
public postData(data){
return new Promise((resolve, reject) => {
this.http.post('https://jsonplaceholder.typicode.com/posts',data,{headers:this.headers})
.subscribe((res:Response)=>{
resolve(res.json())
},err=>{
console.dir(err)
reject()
});
});
}

}

3.在home.ts中引入api服务并调用get和post请求

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LoginPage } from "../login/login";//引入页面
import { StorageProvider } from "../../providers/storage/storage";//引入服务
import { ApiProvider } from "../../providers/api/api";//引入服务

//定义图片格式接口
interface Photo {
albumId: number;
id:number;
title:string;
url:string;
thumbnailUrl:string;
}
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
msg:string;//提示信息
isLogin:boolean=false;//是否登录
list:Array<Photo>=[];
constructor(
public navCtrl: NavController,
public storage:StorageProvider,
public api:ApiProvider
) {
this.init();
}
init(){
//判断是否登录
this.isLogin=this.storage.getItem('isLogin');
let account=this.storage.getItem('account');
if(account){
this.msg=`欢迎你,${account}`;
this.getList();
}else{
this.msg=`还没登录呢,大兄弟。`;
}
}
getList(){
//获取list用于显示
this.api.getList().then(data=>{
console.dir(data);
this.list=<any>data;
});
//测试post请求
let data=JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1
});
this.api.postData(data).then(data=>{
console.dir(data);
});
}
goLoginPage(){
this.navCtrl.push(LoginPage);//路由跳转-前进
}
logout(){
//删除localStorage里面的登录信息
this.storage.removeItem('isLogin');
this.storage.removeItem('account');
this.init();
}
}

4.在home.html中以图文列表格式显示

<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>

<ion-content padding>
<h2>{{msg}}</h2>
<button ion-button class="btn-login" color="secondary" *ngIf="!isLogin" (click)="goLoginPage()">去登录页面</button>
<button ion-button class="btn-login" *ngIf="isLogin" (click)="logout()">退出登录</button>
<ion-list>
<ion-item *ngFor="let item of list">
<ion-avatar item-left>
<img [src]="item?.url">
</ion-avatar>
<h2>{{item?.title}}</h2>
<p>{{item?.url}}</p>
<button ion-button clear item-right>查看详情</button>
</ion-item>
</ion-list>
</ion-content>

5.结果如图:

三、滑动列表
1.修改contact.ts和contact.html

//contact.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

//定义学生格式接口
interface Student {
id: number;
name:string;
describe:string;
src:string;
}
@Component({
selector: 'page-contact',
templateUrl: 'contact.html'
})
export class ContactPage {
public classList:Array<Student>=[
{id:1,name:'aaa',describe:'aaaaaa',src:'assets/imgs/a1.jpg'},
{id:2,name:'bbb',describe:'bbbbbb',src:'assets/imgs/a2.jpg'},
{id:3,name:'ccc',describe:'cccccc',src:'assets/imgs/a3.jpg'},
{id:4,name:'ddd',describe:'dddddd',src:'assets/imgs/a4.jpg'},
{id:5,name:'eee',describe:'eeeeee',src:'assets/imgs/a5.jpg'},
];
constructor(public navCtrl: NavController) {

}

}

//contact.html
<ion-header>
<ion-navbar>
<ion-title>
Contact
</ion-title>
</ion-navbar>
</ion-header>

<ion-content>
<ion-list>
<ion-item-sliding *ngFor="let item of classList">
<ion-item>
<ion-thumbnail item-start>
<img [src]="item?.src">
</ion-thumbnail>
<h2>{{item?.name}}</h2>
<p>{{item?.describe}}</p>
</ion-item>
<ion-item-options side="left">
<button ion-button color="primary">
<ion-icon name="text"></ion-icon>
Text
</button>
<button ion-button color="secondary">
<ion-icon name="call"></ion-icon>
Call
</button>
</ion-item-options>
<ion-item-options side="right">
<button ion-button color="danger">
<ion-icon name="mail"></ion-icon>
Email
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>

2.效果如图所示

---------------------
作者:漫疏狂
来源:CSDN
原文:https://blog.csdn.net/lyt_angularjs/article/details/81145468?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!

[转]ionic3项目实战教程三(创建provider、http请求、图文列表、滑动列表)的更多相关文章

  1. Selenium Web 自动化 - 项目实战(三)

    Selenium Web 自动化 - 项目实战(三) 2016-08-10 目录 1 关键字驱动概述2 框架更改总览3 框架更改详解  3.1 解析新增页面目录  3.2 解析新增测试用例目录  3. ...

  2. MySQL数据库企业集群项目实战(阶段三)

                              MySQL数据库企业集群项目实战(阶段三) 作者 刘畅 时间 2020-10-25 目录 1 架构拓扑图 1 1.1 方案一 1 1.2 方案二 2 ...

  3. Python Django CMDB项目实战之-3创建form表单,并在前端页面上展示

    基于之前的项目代码 Python Django CMDB项目实战之-1如何开启一个Django-并设置base页.index页.文章页面 Python Django CMDB项目实战之-2创建APP. ...

  4. Vue2+VueRouter2+webpack 构建项目实战(三):配置路由,运行页面

    制作.vue模板文件 通过前面的两篇博文的学习,我们已经建立好了一个项目.问题是,我们还没有开始制作页面.下面,我们要来做页面了. 我们还是利用 http://cnodejs.org/api 这里公开 ...

  5. (转载)Android项目实战(三十二):圆角对话框Dialog

    Android项目实战(三十二):圆角对话框Dialog   前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话 ...

  6. Vue2+VueRouter2+webpack 构建项目实战(四)接通api,先渲染个列表

    Vue2+VueRouter2+webpack 构建项目实战(四)接通api,先渲染个列表:  Vue2+VueRouter2+Webpack+Axios 构建项目实战2017重制版(一)基础知识概述

  7. mybatis实战教程三:多对多关联

    MyBatis3.0 添加了association和collection标签专门用于对多个相关实体类数据进行级联查询,但仍不支持多个相关实体类数据的级联保存和级联删除操作 一.创建student.te ...

  8. Node+Express+MongoDB + Socket.io搭建实时聊天应用实战教程(三)--前后端环境配置

    前言 之前都是介绍一些基础知识,在这一节,我们就要开始实战coding了.正所谓磨刀不误砍柴工,准备工作显得尤为重要.很多demo只是追求效果的实现,并不注重整个demo的架构性.从我个人的角度看来, ...

  9. Spring Boot +Vue 项目实战笔记(三):数据库的引入

    这一篇的主要内容是引入数据库并实现通过数据库验证用户名与密码. 一.引入数据库 之前说过数据库的采用是 MySQL,算是比较主流的选择,从性能和体量等方面都比较优秀,当然也有一些弊端,但数据库不是我们 ...

随机推荐

  1. nginx图解

    1.Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理. 这里我给来2张图,对正向代理与反响代理做个诠释,具体细节,大家可以翻阅下资料. Nginx在做反向代理时,提供性能稳定, ...

  2. python运算符优先级

    下面这个表给出Python的运算符优先级,从最低的优先级(最松散地结合)到最高的优先级(最紧密地结合).这意味着在一个表达式中,Python会首先计算表中较下面的运算符,然后在计算列在表上部的运算符. ...

  3. python基本数据类型之列表和元组

    python基本数据类型之列表与元组 python中list与tuple都是可以遍历类型.不同的是,list是可以修改的,而元组属于不可变类型,不能修改. 列表和元组中的元素可以是任意类型,并且同一个 ...

  4. segmentController

    xml <view class="segmentedControl">     <!-- 循环遍历 -->     <block wx:for=&qu ...

  5. IDEA运行android项目一直是同一个apk

    1.IDEA运行android项目时不像eclipse和android studio那样直接运行,IDEA需要设置Artifacts,这样每次运行的时候它才能重新编译,以下附上步骤! 这样就可以了.

  6. Do More With These Great Plugins for Windows Live Writer(old)

    This article is out of day,now we use open live wirter, but we don’t have so much works great plugin ...

  7. 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体

    [源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...

  8. 数组的三种声明方式总结、多维数组的遍历、Arrays类的常用方法总结

    1. 数组的三种声明方式 public class WhatEver { public static void main(String[] args) { //第一种 例: String[] test ...

  9. linux内核中GNU C __attribute__ 机制的实用

    很多东西,只看看是不行的,要想深入的去了解一个东西,一定要去不断地学习,实践,反思. 说白了就是要去打磨. 在linux中,最近遇到了这样一个定义: int board_usb_init(int in ...

  10. vue安装及axios、stylus、iview的安装流程整理

    现在做的项目中主要用到以下几个安装包,所以整理下流程: 使用命令行工具npm新创建一个vue项目 vue中axios的安装和使用 在vue项目中stylus的安装及使用 如何在vue中全局引入styl ...