Search code examples
angularangular2-diangular2-injection

Angular2 Inject Http Without Bootstrap


Context:

I have a class that is going to form POST requests for my REST Api, lets call it requester. Every ajax call made in my app will be sent through this. Thus I don't want to provide Http via bootstrap(foo, [HTTP_PROVIDERS]); That would make it global and I need it to be injected to requester

Code:

import {Http} from '@angular2/http';
@Injectable()
@Component({providers: [Http]})
export class requester {
    constructor(private http:Http){}
    ...
}

Error:

ORIGINAL EXCEPTION: No provider for Http! (requestor -> Http)


Solution

  • solve it like this

    import { Http, Response } from '@angular/http';
    export class LoginService {
    
      constructor(private http: Http) {
    
      }
    }
    

    or use it like this

    public constructor(@Inject(Http)  private http: Http) {}