美文网首页
anguar2-http实例

anguar2-http实例

作者: 擂鼓人 | 来源:发表于2016-03-11 17:52 被阅读223次

这个组件全部放在一个文件夹中,先讲下编写组件的思路吧,其中也遇到不少坑

  • 既然是编写组件当时首先是创建一个单独的子文件夹,先写一个类似hello world的简单例子,在页面跑起来,再一步步添加东西;
    </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是在初始化时获取相关数据,这里是用HeroService的方法获取模板渲染的数据
this._http.getHeroes().then(data => this.heroes = data);
}

public heroes: Hero[];

//定义添加newHero方法,加入heroes数组中
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定义服务属性,使用Rxjs发送异步请求获取数据
<pre>
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import {Hero} from './hero'; //导入数据结构
import 'rxjs/Rx'; //导入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定义数据结构接口
<pre>
export interface Hero {
name: string;
id: number;
}
</pre>
5.herodata.json<Hero[]>类型的数组数据
<pre>
[{"name": "SpiderMan", "id": 1},
{"name": "Kongfu Pander", "id": 2},
{"name": "Icon Man", "id": 3},
{"name": "Gorilla", "id": 4}]
</pre>
.注:页头要引入http.dev.js否则报错
<pre>
<script src="node_modules/angular2/bundles/http.dev.js"></script>
</pre>

相关文章

  • anguar2-http实例

    这个组件全部放在一个文件夹中,先讲下编写组件的思路吧,其中也遇到不少坑 既然是编写组件当时首先是创建一个单独的子文...

  • SQL C语言基本操作

    相关API 打开 实例 关闭 实例 获取错误消息 操作表 实例创建 实例插入 实例修改 实例删除 实例回调查询 非回调

  • Python-数据类型及其操作方法

    数字类型 代码实例: 字符串类型 代码实例: 列表 代码实例: 元组 代码实例 字典: 代码实例 集合 代码实例:

  • HTML基础-03

    HTML 标题 实例 HTML 段落 实例 HTML 链接 实例 HTML 图像 实例

  • Python 类属性、实例属性、类方法、实例方法

    1、实例属性 实例属性,就是赋给由类创建的实例的属性,实例属性属于它所属的实例,不同实例之间的实例属性可以不同。 ...

  • STL算法之常用拷贝和替换

    copy API 实例 replace API 实例 replace_if API 实例 swap API 实例

  • Vue 基础

    Vue 实例 1. Vue实例 2. 实例属性 3. 实例方法/数据 4. 实例方法/事件 5. 实例方法/生命周...

  • 类中的方法

    1.实例方法的调用方式 实例对象.实例方法() 类对象.实例方法(实例对象) 例如: class Student ...

  • C语言100例

    C 练习实例01C 练习实例02C 练习实例03C 练习实例04C 练习实例05C 练习实例06 C 练习实例07...

  • AWS云计算助手级架构师认证之EC2-实例购买类型

    在购买EC2实例的时候,这里有三种购买类型需要理解:按需实例,预留实例,计划实例,竞价实例。 按需实例: ...

网友评论

      本文标题:anguar2-http实例

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