欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

angularjs http與后臺交互的實現(xiàn)示例

 更新時間:2018年12月21日 09:56:15   作者:liukai90  
這篇文章主要介紹了angularjs http與后臺交互的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.描述

無論是使用angularjs做前端或是結(jié)合ionic混合開發(fā)移動端開發(fā)app都需要與后臺進行交互,而angular給我提供了httpModule模塊供我們使用。今天就展現(xiàn)一個http的封裝和使用的一個具體流程。

2. HttpModule引入

找到app.module.ts文件

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';


import { LoginPage } from "../pages/login/login";
/**
引入HttpClientModule模塊
*/
import { HttpClientModule } from "@angular/common/http";

import { RequestServiceProvider } from "../providers/request-service/request-service";
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

@NgModule({
 declarations: [
  MyApp,
  
  LoginPage,
  
 ],
 imports: [
  BrowserModule,
   /**
   導(dǎo)入模塊
   */
  HttpClientModule,
  
  IonicModule.forRoot(MyApp,{
   tabsHideOnSubPages:'true',
   backButtonText:''
  })
 ],
 bootstrap: [IonicApp],
 entryComponents: [
  MyApp,
  
  LoginPage,
  
 ],
 providers: [
  StatusBar,
  SplashScreen,
  {provide: ErrorHandler, useClass: IonicErrorHandler},
  RequestServiceProvider,
  
 ]
})
export class AppModule {}

按照自己的項目導(dǎo)入HttpClientModule模塊即可,我導(dǎo)入其他組件,不用考慮。

3.創(chuàng)建服務(wù)

ionic g provider RequestService

執(zhí)行完成后則會出現(xiàn)如下文件

4.封裝服務(wù)

/**
導(dǎo)入http相關(guān)
*/
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Observable} from "rxjs";

/*
 Generated class for the RequestServiceProvider provider.

 See https://angular.io/guide/dependency-injection for more info on providers
 and Angular DI.
*/
@Injectable()
export class RequestServiceProvider {

  /**
  講基礎(chǔ)路徑提取說出來,配置ip和端口時只需要在這修改
  */
 //basePath:string='http://10.4.0.205:8081'
 reserveBasePath:string='http://10.6.254.110:8081'
 basePath=this.reserveBasePath;
  /**
  封裝固定的消息頭相關(guān)
  */
 private headers = new HttpHeaders({'Content-Type': 'application/json'})
 // private headers = new HttpHeaders({'Access-Control-Allow-Origin':'*'});

/**
初始化http變量
*/
 constructor(public http: HttpClient) {
  console.log('Hello RequestServiceProvider Provider');
 }

  /**
  給外界提供了四個基礎(chǔ)的方法只需要傳入uri和data即可
  */
 get(req:any):Observable<any> {
  return this.http.get(this.basePath+req.uri,{headers:this.headers});
 }

 post(req:any):Observable<any>{
  return this.http.post(this.basePath+req.uri,req.data,{headers:this.headers});
 }
 put(req:any):Observable<any>{
  return this.http.put(this.basePath+req.uri,req.data,{headers:this.headers});
 }
 delete(req:any):Observable<any>{
  return this.http.delete(this.basePath+req.uri,{headers:this.headers});
 }

}

5.導(dǎo)入聲明封裝服務(wù)

找到app.module.ts文件和第一部類似

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';


import { LoginPage } from "../pages/login/login";
/**
引入HttpClientModule模塊
*/
import { HttpClientModule } from "@angular/common/http";

/**
導(dǎo)入自定的服務(wù)
*/
import { RequestServiceProvider } from "../providers/request-service/request-service";
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

@NgModule({
 declarations: [
  MyApp,
  
  LoginPage,
  
 ],
 imports: [
  BrowserModule,
   /**
   導(dǎo)入模塊
   */
  HttpClientModule,
  
  IonicModule.forRoot(MyApp,{
   tabsHideOnSubPages:'true',
   backButtonText:''
  })
 ],
 bootstrap: [IonicApp],
 entryComponents: [
  MyApp,
  
  LoginPage,
  
 ],
 providers: [
  StatusBar,
  SplashScreen,
  {provide: ErrorHandler, useClass: IonicErrorHandler},
  /**
  聲明服務(wù)
  */
  RequestServiceProvider,
  
 ]
})
export class AppModule {}

6.使用服務(wù)

找到自己的頁面所對應(yīng)的ts文件如下面代碼一樣

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
/**
導(dǎo)入聲明
*/
import {RequestServiceProvider} from "../../providers/request-service/request-service";

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
 selector: 'page-login',
 templateUrl: 'login.html',
})
export class LoginPage {
 title:string = '登錄'
 promptMessage:string = ''

 user={
  username:'',
  password:''
 }
 req={
  login:{
   uri:'/user/login'
  }

 }

 constructor(public navCtrl: NavController, public navParams: NavParams,
        /**
        初始化服務(wù)對象
        */
       private requestService:RequestServiceProvider) {

 }
 
 ionViewDidLoad() {
  console.log('ionViewDidLoad LoginPage');
 }
 login(){
 
   /**
   調(diào)用post方法,subscribe()方法可以出發(fā)請求,調(diào)用一次發(fā)送一次,調(diào)用多次發(fā)多次
   */
  this.requestService.post({uri:this.req.login.uri,data:user}).subscribe((res:any)=>{
   console.log(res);
   if (res.code == 0){
    this.promptMessage = res.message;
   } else {   
    this.promptMessage = res.message;
   }

  },
   error1 => {
   alert(JSON.stringify(error1))
   });

 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Angular中ng-repeat與ul li的多層嵌套重復(fù)問題

    Angular中ng-repeat與ul li的多層嵌套重復(fù)問題

    這篇文章主要介紹了Angular中ng-repeat與ul li的多層嵌套重復(fù)問題,需要的朋友可以參考下
    2017-07-07
  • jQuery和AngularJS的區(qū)別淺析

    jQuery和AngularJS的區(qū)別淺析

    這篇文章主要介紹了jQuery和AngularJS的區(qū)別淺析,本文著重講解一個熟悉jQuery開的程序員如何應(yīng)對AngularJS中的一些編程思想的轉(zhuǎn)變,需要的朋友可以參考下
    2015-01-01
  • angular多語言配置詳解

    angular多語言配置詳解

    這篇文章主要介紹了angular多語言配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • AngularJS表單和輸入驗證實例

    AngularJS表單和輸入驗證實例

    本篇文章詳細的介紹了AngularJS表單和輸入驗證實例, AngularJS表單可以提供驗證功能。有需要的可以了解一下。
    2016-11-11
  • Angular2 組件交互實例詳解

    Angular2 組件交互實例詳解

    Angular2應(yīng)用程序?qū)嶋H上是有很多父子組價組成的組件樹,因此,了解組件之間如何通信,特別是父子組件之間,對編寫Angular2應(yīng)用程序具有十分重要的意義。下面通過本文給大家介紹Angular2 組件交互知識,感興趣的朋友一起看看吧
    2017-08-08
  • Angularjs實現(xiàn)上傳圖片預(yù)覽功能

    Angularjs實現(xiàn)上傳圖片預(yù)覽功能

    本文通過實例代碼給大家介紹了Angularjs實現(xiàn)上傳圖片預(yù)覽功能,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-09-09
  • 詳解為Angular.js內(nèi)置$http服務(wù)添加攔截器的方法

    詳解為Angular.js內(nèi)置$http服務(wù)添加攔截器的方法

    所謂攔截器就是在目標達到目的地之前對其進行處理以便處理結(jié)果更加符合我們的預(yù)期。Angular的$http攔截器是通過$httpProvider.interceptors數(shù)組定義的一組攔截器,每個攔截器都是實現(xiàn)了某些特定方法的Factory。本文就介紹了為Angular.js內(nèi)置$http服務(wù)添加攔截器的方法。
    2016-12-12
  • Angular父組件調(diào)用子組件的方法

    Angular父組件調(diào)用子組件的方法

    組件是一種特殊的指令,使用更簡單的配置項來構(gòu)建基于組件的應(yīng)用程序架構(gòu).這篇文章主要介紹了Angular組件——父組件調(diào)用子組件方法,需要的朋友可以參考下
    2018-04-04
  • Angular使用$http.jsonp發(fā)送跨站請求的方法

    Angular使用$http.jsonp發(fā)送跨站請求的方法

    這篇文章主要介紹了Angular使用$http.jsonp發(fā)送跨站請求的方法,結(jié)合實例形式分析了$http.jsonp發(fā)送跨站請求遇到的問題與相應(yīng)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • AngularJS基礎(chǔ) ng-mouseleave 指令詳解

    AngularJS基礎(chǔ) ng-mouseleave 指令詳解

    本文主要介紹AngularJS ng-mouseleave 指令,這里幫大家整理了ng-mouseleave指令的詳細資料,并附有代碼示例,有需要的小伙伴可以參考下
    2016-08-08

最新評論