角2 ES6注射Http [英] Angular 2 ES6 Inject Http

查看:113
本文介绍了角2 ES6注射Http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力弄清楚如何使用es6将http注入我的课堂。当我使用@inject时,我收到一条错误,说 inject未定义。我有没有其他的东西我必须导入注入在这里工作?

I am trying to figure out how to properly inject http into my class using es6. When I use @inject I get an error saying inject is not defined. Am I missing something else I must import for inject to work here?

import 'zone.js/lib/browser/zone-microtask';
import 'reflect-metadata';
import 'babel-polyfill';

import {provide} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS, LocationStrategy, HashLocationStrategy} from 'angular2/router';
import {Http, Headers} from 'angular2/http';

import {Component, View, Input} from 'angular2/core';
import {RouteConfig, RouteParams, ROUTER_DIRECTIVES} from 'angular2/router';


@Component({
    selector: 'test-app',
    template: '<div>Hello my name is {{name}}. <button (click)="sayMyName()">Say my name</button></div>'
})
@inject('Http')
export class TestApp
{
    constructor(http)
    {
        this.name = 'Allencoded';
        this.http = http;
    }

    sayMyName()
    {
        console.log('My Name is ', this.name);
        console.log(this.http);
    }
}


推荐答案

是如何得到它终于工作:

This is how I got it to finally work:

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

export class TestApp
{
    static get parameters() {
        return [[Http]];
    }

    constructor(http)
    {
        this.name = 'Allen';
        this.http = http;
    }
}

bootstrap(TestApp, [
  HTTP_PROVIDERS,
  provide(LocationStrategy, { useClass: HashLocationStrategy })
]);

取自:
如何将Angular2 Http服务注入es6 / 7类?

这篇关于角2 ES6注射Http的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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