1

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);
    }
}
allencoded
  • 7,015
  • 17
  • 72
  • 126

1 Answers1

3

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 })
]);

Taken from: How to Inject Angular2 Http service into es6/7 Class?

Community
  • 1
  • 1
allencoded
  • 7,015
  • 17
  • 72
  • 126