anguar2-http實(shí)例

這個(gè)組件全部放在一個(gè)文件夾中,先講下編寫組件的思路吧,其中也遇到不少坑

  • 既然是編寫組件當(dāng)時(shí)首先是創(chuàng)建一個(gè)單獨(dú)的子文件夾,先寫一個(gè)類似hello world的簡單例子,在頁面跑起來,再一步步添加?xùn)|西;
    </br>
  1. http.component.ts主文件,在HttpComponent組件模板中引入HeroList子組件。
    <pre>
    import {Component} from 'angular2/core';
    import {HeroList} from './hero-list.component';
    import {Hero} from './hero';
    @Component({
    selector: 'http-component',
    template: <h1>Tour of Heroes !</h1> <hero-list></hero-list>,
    directives: [HeroList], //此處必須注入依賴的子組件
    providers: [HTTP_PROVIDERS] // 也可在bootstrap中添加,與ROUTER_PROVIDERS一樣
    })
    export class HttpComponent {}
    </pre>

  2. hero-list.ts文件,定義HeroList組件模板
    <pre>
    import {Component,OnInit,Injectable} from 'angular2/core';
    import {HTTP_PROVIDERS} from 'angular2/http';
    import {HeroService} from './hero.service';
    import {Hero} from './hero';
    @Component({
    selector: 'hero-list',
    template: <h3>Heroes:</h3> <ul> <li *ngFor="#hero of heroes">{{hero.name}}</li> </ul> New Hero: <input #newHero/> <button (click)="addHero(newHero)">Add Hero</button>,
    providers: [
    HTTP_PROVIDERS,
    HeroService // 依賴的文件必須提供
    ]
    })
    export class HeroList implements OnInit{
    constructor(private _http: HeroService) {}

ngOnInit() { //OnInit是在初始化時(shí)獲取相關(guān)數(shù)據(jù),這里是用HeroService的方法獲取模板渲染的數(shù)據(jù)
this._http.getHeroes().then(data => this.heroes = data);
}

public heroes: Hero[];

//定義添加newHero方法,加入heroes數(shù)組中
addHero(hero) {
if(!hero.value) return;
let newHero: Hero = {
name: hero.value,
id: this.heroes.length
}
this.heroes.push(newHero);
hero.value = "";
}
}
</pre>
3.hero-sevice.ts定義服務(wù)屬性,使用Rxjs發(fā)送異步請求獲取數(shù)據(jù)
<pre>
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import {Hero} from './hero'; //導(dǎo)入數(shù)據(jù)結(jié)構(gòu)
import 'rxjs/Rx'; //導(dǎo)入rx模塊提供一步請求用
@Injectable()
export class HeroService {
constructor(private http: Http) {}
public heroes: Hero[];
private _heroUrl = "app/http-client/heroes.json";
getHeroes() {
return this.http.get(this._heroUrl)
.toPromise()
.then(res => <Hero[]>res.json());
}
}
</pre>
4.hero.ts定義數(shù)據(jù)結(jié)構(gòu)接口
<pre>
export interface Hero {
name: string;
id: number;
}
</pre>
5.herodata.json<Hero[]>類型的數(shù)組數(shù)據(jù)
<pre>
[{"name": "SpiderMan", "id": 1},
{"name": "Kongfu Pander", "id": 2},
{"name": "Icon Man", "id": 3},
{"name": "Gorilla", "id": 4}]
</pre>
.注:頁頭要引入http.dev.js否則報(bào)錯(cuò)
<pre>
<script src="node_modules/angular2/bundles/http.dev.js"></script>
</pre>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Angular 2架構(gòu)總覽 - 簡書http://m.itdecent.cn/p/aeb11061b82c A...
    葡萄喃喃囈語閱讀 1,554評論 2 13
  • 版本:Angular 5.0.0-alpha AngularDart(本文檔中我們通常簡稱 Angular ) 是...
    soojade閱讀 924評論 0 4
  • 版本:4.0.0+2 有一些英雄指南應(yīng)用的新需求: 添加一個(gè)儀表盤 視圖。 添加在英雄 視圖和 儀表盤 視圖之間導(dǎo)...
    soojade閱讀 1,459評論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,711評論 19 139
  • 當(dāng)你去問打工一族,遠(yuǎn)離家鄉(xiāng)打工是為了什么?可能98%的人都說是為了掙錢,然后你又想過,掙到錢了之后,做什么呢?這個(gè)...
    秋爺職場官閱讀 435評論 0 0

友情鏈接更多精彩內(nèi)容