angular2-英雄指南教程http请求

看这个之前,你要确保你的教程没错,能跑起来。最近好多人都遇到http请求问题。不知道怎么跟后台接口对接。首先,在我们的英雄指南教程里面,有个模拟的假数据,我们不需要导入他,那么在app.module.ts里面,我们需要把导入的angular-in-memory-web-api注释掉或者删掉,同时需要在下面@NgModule里面的imports:里找到如下代码删掉即可:InMemoryWebApiModule.forRoot(InMemoryDataService),然后我们需要在hero.service.ts里面更改如下:

@Injectable()
export class HeroService {
    private heroesUrl = 'http://10.1.1.80:8080/test1'; 此处为后台接口更改为贵公司接口即可。)
}
这样你的英雄列表是从后台获取的了,而后,我们也可以 hero-detail.component.html里加个测试按钮,hero-detail.component.ts里面写个点击事件测试:

<button (click)="test()">test</button>

test(): void{
    this.http.get('http://10.1.1.80:8080/test1')
   .toPromise().then((response) => {
     console.log(response.json());
    });
}

这里需要注意的是,你需要在hero-detail.component.ts里面导入如下:

import { Http ,Response} from '@angular/http';
import 'rxjs/add/operator/toPromise';
constructor里面需要写:private http: Http。这样就OK了。。

我还写了个函数测试调用= =:

test2():void{
    alert("test3");
}
kf():void{
    alert("777");
this.test2();
}
test(): void{
    this.http.get('http://10.1.1.80:8080/test1')
    this.kf();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值