Http is not defined in angular2

I am trying to make a REST Api call to my other local server, I made this service:

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import {GlobalService} from '../app.service';

@Injectable()
export class AuthenticationService {
    constructor(private _globals: GlobalService) {}
    constructor(private _http: Http) {}
    globals = this._globals.getGlobals()

    getAuthenticationData() {
        this._http.get(globals.apiURL)
    }
}

And calling it in my component:

import { Component } from 'angular2/core';
import {AuthenticationService} from '../services/authentication.service';

@Component({
    selector: 'wd-header';
    templateUrl: 'app/templates/header.html'
    providers: [AuthenticationService]
})

export class HeaderComponent {
    constructor(private _authenticationService: AuthenticationService) {}
    authentication = this._authenticationService.getAuthenticationData()
}

For some reason, however, Angular claims that Http is undefined:

EXCEPTION: An error occurred while creating the HeaderComponent! Component. angular2.dev.js: 22911: 9

ORIGINAL EXCLUSION: TypeError: this._http undefined

What am I doing wrong?

Edit to enable main.ts:

import {bootstrap}    from 'angular2/platform/browser';

import {ROUTER_PROVIDERS} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
import {App} from './app.component';
import {GlobalService} from './app.service';

bootstrap(App, [
    ROUTER_PROVIDERS,
    HTTP_PROVIDERS,
    GlobalService
]);
+4
source share
2 answers

I would reorganize your service code in this way:

@Injectable()
export class AuthenticationService {
  constructor(private _globals: GlobalService, private _http: Http) {
    this.globals = this._globals.getGlobals();
  }

  getAuthenticationData() {
    return this._http.get(this.globals.apiURL);
  }
}

You should have only one constructor for your service and initialize the globals property in this constructor.

"this" .

+2

, , AuthenticationService.

:

constructor(private _globals: GlobalService, private _http: Http) {}

return getAuthenticationData().

, .

+2

All Articles