第六章 HTTP

简介
  1. JavaScript中通常有三种处理异步代码的方式。回调(callback),承诺(Promise),可观察对象(observable)。
  2. httpAngular中被拆分为一个单独的模块,因此需要从Angular中导入一些常量。
import { Http, Response, RequestOption, Headers} from '@angular/http'
  1. app.ts中需要导入HttpModule模块。
import { HttpModule} from '@angular/http';

@NgModule({
  declarations:[],
  imports:[HttpModule],
  bootStrap: [HttpApp],
  providers: []
})
  1. 把服务注入组件中
class MyFooComponent {
  constructor(public http: Http){
    
    makeRequest(): void{
      
    }
  }
}
基本请求

1.首先导入一些模块,然后指定@Component的selector。

import { Http , Response } from  '@angular/http';
import {Component} from '@angular/http';

@Component({
  selector: 'simple-http',
})
  1. 构建视图
template:`
	<h2>Basic Request</h2>
	<button type="button" (click)="makeRequest()">make request</button>
	<div *ngIf="loading..."></div>
	<pre>{{data | json}}</pre>
`
  1. 构建控制器
export class SimpleHttpComponent{
  data :Object;
  loading: boolean;
  
  constructor(
  	private http : Http
  ){}
  
  makeRequest() : void{
    this.loading= true;
    this.http.request('http://jsonplaceholder.typicode.com/postts/1')
    .subscribe((res: Response)=>{
      this.data = res.json();
      this.loading =false;
    })
  }
}
  1. 当我们调用makeRequest时,首先要设置this.loading= true。这会在页面上显示云载入指示器。

  2. http请求的方式:调用this.http.request并传入URL作为GET请求的参数。http.request会返回一个Observable对象,可以使用subscribe订阅变化。

  3. http.request从服务器返回一个流时,它就会发出一个Response对象。我们用json方法提取出响应体并解析成一个Object,然后将这个Object赋值给this.data

  4. 只要我们得到了响应,就不需要加载任何东西了,所心就需要设置this.loading=false

编写YouTubeSearchComponent
  1. 编写SearchResult,用obj?: any来模拟关键词参数。在构造ideoUrl时使用了硬编码。
class SearchResult {
  id : string;
  title : string;
  description : string;
  thumbnailUrl: string;
  videoUrl: string;
  
  consturctor(obj?: any){
    this.id = obj && obj.id || null;
    this.title = obj && obj.title || null;
    this.description = obj && obj.description || null;
    this.thumbnailUrl = obj && obj.thumbnailUrl || null;
    this.videoUrl = obj && obj.videoUrl || `https://www.youtube.com/watch?v=${this.id}`;
  }
}
  1. 编写YouTubeService

YouTubeService设置两个用来表示API密钥和API URL的常量

let YOUTUBE_API_KEY: string ="XXXXX";
let YOUTUBE_API_URL :string = "https://www.googleapis.com/youtube/v3/search";

为了解决环境配置问题,注入这些常量

我们要获取http.get的返回值,并用map来从请求中获取Response,这里使用.json()从response中提取返回体并同时实例化成一个对象,然后遍历每个项目并将其转换成一个SearchResult

(<any>response.json()).items是告诉ts,我们并不想在这里进行严格的类型检查,因为当我们使用JSON API时通常并没有API响应体的类型定义信息,所以ts不知道返回的object中会有一个items键,编译器就会出错。

export var youTbueServicrInjectables: Array<any> =[  {provide: YouTubeService, useClass: YouTbueService},  {provide: YOUTUBE_API_KEY, useClass: YOUTUBE_API_KEY}{provide: YOUTUBE_API_URL, useClass: YOUTUBE_API_URL}]@Injectable()export class YouBubeService{  constructor(  	private http: Http,    	@Injectable(YOUTUBE_API_KEY) private apiKey: string,     	@Injectable(YOUTUBE_API_URL) private apiUrl: stirng    ){}    search(query: string):Observable<SearchResult[]>{    let params: string =[      `q=${query}`,      `key=${this.apiKey}`,      `part=snippet`,      `maxRseutls=10`    ].join('&');    let queryUrl:string =`${this.apiUrl}?${params}`;        return this.http.get(queryurl).map((response :Response)=>[      return (<any>response.json()).items.map(item=>{      	return new SearchResult({          id: item.id.videoId,          title: item.snippet.title,          desciption: item.snippet.description,          thumbnailUrl: item.snippet.thumbnails.high.url        })    })    ])  }}

app.ts中使用

import { HttpModule} from "@angular/http";import { youTubeServiceInJectables} from "components/YouTubeSearchComponent";@NgModule({  declarations: [    HttpApp  ],  imports:[ BrowserModule, HttpModule],  providers:[ youTubeServiceInJectables]})class HttpAppModule{}
  1. 编写SearchBox

    我们通过implements oninit让这个类实现对应的接口,这么做是因为需要使用生命周期中的ngOninit的回调,如果一个类声明implements oninit,那么ngOninit函数会在首次变化检查后调用。

@Component({  outputs:['loading', 'result'],  selector:'search-box',  template:`		<input type="text" class="form-control" placeholder="Search" autofocus>`})export class SearchBox implements Oninit{  	loading: EventEmitter<boolean> = new EventEmitter <boolean>();     results: EventEmitter<SearchResult[]>=new EventEmitter<SearchResult[]>();    	constructor(  		private youtube: YouTubeService,         private el : ElementRef,  ){}    	ngOninit():void{      Obesrvable.fromEvent(this.el.nativeElement,'keyUP')      .map((e:any)=>e.target.value      .filter((text:string)=>text.length>1)       .debounceTime(250)       .do(()=>this.loading.next(true))       .map((query:string)=>this.youtube.search(query)).        .switch()        .subscribe(        		(results: SearchResult[])=>{        	this.loading.next(false);        	this.results.next(results);      },        		(err:any)=>{              this.loading.next(false);            },        	()=>{            this.loading.next(false);          }                               )          )    }}
<search-box      (loading)="loading=$event"       (results)="updateResults($event)"            ></search-box>
  1. 发送post请求
makepost(): void{  this.loading=true;  this.http.post(  'http://jsonplaceholder.typicode.com/posts',  JSON.stringfy({  	body: 'bar',  	title: 'foo',  	userId: 1})  )  .subscribe((res: Response)=>{    this.data=res.json();    this.loading=false;  })}
  1. 发送delete请求
makeDelet():void{  this.loading=true;  this.http.delete(  	'http://jsonplaceholder.typicode.com/posts/1'  )  .subscribe((res:Response)=>{    this.data=res.json;    this.loading= false;  })}
  1. 携带可选的末位参数
makeHeaders():void{  let headers: Headers =new Headers();  headers.append('X-API-TOKEN', 'ng-book');	let opts: RequestOptions = new RequestOptions();	opts.headers=headers;		this.http.get(  	'http://jsonplaceholder.typicode.com/posts/1',    	opts)	.subscribe((res: Response)=>{    this.data =res.json();      })}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值