gpt4 book ai didi

angular - 使用 Http JSON AngularJS 2 时出错

转载 作者:太空狗 更新时间:2023-10-29 18:14:27 25 4
gpt4 key购买 nike

在尝试了几种无法使它工作的在线教程方法后,我尝试使用以下代码。

..//
import {Http, HTTP_PROVIDERS} from 'angular2/http';

@Component({

viewProviders: [HTTP_PROVIDERS],

template: `
..//
<div class="col-md-6 col-md-offset-3 well">

<input type="button" class="btn btn-success btn-block"
(click)="get()" value="Fetch Users">

<hr>
<ul>
<li *ngFor="#user of users">
Id: {{ user.name }} | Name: {{ user.name }} | JSON: {{ user | json }}
</li>
</ul>
</div>

..//

export class Mov {

//Inicio test http

users: Array<Object>;
http: any;

//
//constructor(@Inject(HttpFactory) http) {
//constructor(http: Http) {
constructor(http: Http){
//
this.http = http;
}

get(){
this.http('resources/users.json')
.map(res => res.json())
.subscribe(users => this.users = users);
}

得到这个错误:

EXCEPTION: Error during evaluation of "click"

ORIGINAL EXCEPTION: TypeError: this.http is not a function

任何人都可以告诉我我做错了,我为我的英语感到抱歉。

最佳答案

嗯,由于错误消息指出 this.http 不是函数,它是一个具有多个方法的对象。你需要 this.http.get() 方法:

this.http.get('resources/users.json')
.map(res => res.json())
.subscribe(users => this.users = users);

关于angular - 使用 Http JSON AngularJS 2 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35350130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com