错误使用HTTP JSON AngularJS 2 [英] Error using Http JSON AngularJS 2

查看:91
本文介绍了错误使用HTTP JSON AngularJS 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用下面的code尝试多种方式的在线教程,我不能使它发挥作用了。

I'm trying to use the following code after trying several ways online tutorials I can not make it work.

..//
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"

原始异常:类型错误:this.http不是一个函数

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

谁能告诉我我做错了,我为我的英语很抱歉。

anyone can tell me I'm doing wrong, I'm sorry for my English.

推荐答案

那么,作为错误消息状态 this.http 是不是一个功能,它是一个对象,它具有几种方法。您需要 this.http.get()方法:

Well, as the error message states this.http is not a function, it's an object which has several methods. You need this.http.get() method:

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

这篇关于错误使用HTTP JSON AngularJS 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆