简体   繁体   English

Angular2 + typescript + jspm:没有Http的提供者(App - > Provider - > Http)

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

I trying migrate from Webpack to JSPM with system.js. 我尝试使用system.js从Webpack迁移到JSPM。 We have simple App component. 我们有简单的App组件。 I was fallowing this article Angular 2 Starter Setup with JSPM, SystemJS and Typescript in atom (Part 1) 我正在阅读这篇文章Angular 2 Starter Setup with JSPM,SystemJS和Typescript in atom(第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 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 在Webpack工作正常但在这里使用systemjs我收到此错误

EXCEPTION: No provider for Http! 例外:没有Http的提供者! (App -> Bus -> 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 我读过Angular2没有NameService的提供者,但是他们谈论的是Angular2 alpha而且提供者中没有提供者,我们使用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! 例外:没有ConnectionBackend的提供者! (App -> Http -> ConnectionBackend) (App - > Http - > ConnectionBackend)

I even tryed change to angular2-beta@6. 我甚至尝试过更改为angular2-beta @ 6。 and also angular2-beta@1 Do you know what I do wrong? 还有angular2-beta @ 1你知道我做错了什么吗? thank you 谢谢

Using loader versions: systemjs@0.19.23 使用加载程序版本: systemjs@0.19.23

>= RC.5 > = RC.5

Add HttpModule to imports of the AppModule : HttpModule添加到AppModule导入中:

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

<= RC.5 <= RC.5

You need to add HTTP_PROVIDERS to the providers list in bootstrap 您需要在引导程序中将HTTP_PROVIDERS添加到提供程序列表中

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://angular.io/docs/ts/latest/api/http/HTTP_PROVIDERS-let.html

You need to define the HTTP_PROVIDERS providers when bootstrapping your application: 在引导应用程序时,您需要定义HTTP_PROVIDERS提供程序:

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) { }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM