Angular2 + typescript + jspm : No provider for Http ( App -> Provider -> Http )

别等时光非礼了梦想. 提交于 2019-12-23 10:19:02

问题


I trying migrate from Webpack to JSPM with system.js. We have simple App component. I was fallowing this article Angular 2 Starter Setup with JSPM, SystemJS and Typescript in atom (Part 1)

import {Component} from 'angular2/core';
import {Bus} from './business.service';

@Component({
  selector: 'app',
  template: `
  <p>Hello World</p>
  `,
  providers:[Bus]
})
export class App {
  constructor(private bus : Bus) { }
}

and simple (business) service with Http

import {Injectable} from 'angular2/core';
import {Http, Response, Headers, RequestOptions} from 'angular2/http';
import {Observable}     from 'rxjs/Observable';

@Injectable()
 export class Bus {

  constructor(private http: Http){

  }
}

In Webpack works fine but here with systemjs I getting this error

EXCEPTION: No provider for Http! (App -> Bus -> Http)

I read Angular2 no provider for NameService but they talking about Angular2 alpha and no provider in provider and we use beta@7

I also play with

import {Component} from 'angular2/core';
//import {Bus} from './business.service';
import {Http, Response, Headers, RequestOptions} from 'angular2/http';
@Component({
  selector: 'app',
  template: `
  <p>Hello World</p>
  `,
  providers:[Http]
})
export class App {
  constructor(private bus : Http) { }
}

but the error is even weirder

EXCEPTION: No provider for ConnectionBackend! (App -> Http -> ConnectionBackend)

I even tryed change to angular2-beta@6. and also angular2-beta@1 Do you know what I do wrong? thank you

Using loader versions: systemjs@0.19.23


回答1:


You need to define the HTTP_PROVIDERS providers when bootstrapping your application:

import {HTTP_PROVIDERS} from 'angular2/http';

bootstrap(AppComponent, [ HTTP_PROVIDERS ]);

or if you want to specify it at your component level:

import {Component} from 'angular2/core';
//import {Bus} from './business.service';
import {Http, Response, Headers, RequestOptions, HTTP_PROVIDERS} from 'angular2/http';
@Component({
  selector: 'app',
  template: `
    <p>Hello World</p>
  `,
  providers:[HTTP_PROVIDERS] // <-----------
})
export class App {
  constructor(private bus : Http) { }
}



回答2:


>= RC.5

Add HttpModule to imports of the AppModule:

@NgModule({
  imports: [HttpModule],
  ...
})
export class AppModule {}

<= RC.5

You need to add HTTP_PROVIDERS to the providers list in bootstrap

import {HTTP_PROVIDERS} from 'angular2/http';
<!-- -->
bootstrap(AppComponent, [HTTP_PROVIDERS]);

See also https://angular.io/docs/ts/latest/api/http/HTTP_PROVIDERS-let.html



来源:https://stackoverflow.com/questions/35719236/angular2-typescript-jspm-no-provider-for-http-app-provider-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!