Angular2 + typescript + jspm:没有 Http 的提供者(App -> Provider -> Http)
Angular2 + typescript + jspm : No provider for Http ( App -> Provider -> Http )
我正在尝试使用 system.js 从 Webpack 迁移到 JSPM。我们有简单的 App 组件。我正在阅读这篇文章 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) { }
}
和 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){
}
}
在 Webpack 中工作正常,但在 systemjs 中出现此错误
EXCEPTION: No provider for Http! (App -> Bus -> Http)
我读过 但他们谈论的是 Angular2 alpha 并且 provider 中没有提供者,我们使用 beta@7
我也玩
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) { }
}
但错误更奇怪
EXCEPTION: No provider for ConnectionBackend! (App -> Http -> ConnectionBackend)
我什至尝试更改为 angular2-beta@6。还有 angular2-beta@1 你知道我做错了什么吗?谢谢
使用加载程序版本:systemjs@0.19.23
>=RC.5
将 HttpModule
添加到 AppModule
的导入中:
@NgModule({
imports: [HttpModule],
...
})
export class AppModule {}
<= RC.5
您需要将 HTTP_PROVIDERS
添加到 bootstrap
中的提供商列表
import {HTTP_PROVIDERS} from 'angular2/http';
<!-- -->
bootstrap(AppComponent, [HTTP_PROVIDERS]);
另见 https://angular.io/docs/ts/latest/api/http/HTTP_PROVIDERS-let.html
您需要在引导应用程序时定义 HTTP_PROVIDERS
提供程序:
import {HTTP_PROVIDERS} from 'angular2/http';
bootstrap(AppComponent, [ HTTP_PROVIDERS ]);
或者如果您想在组件级别指定它:
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) { }
}
我正在尝试使用 system.js 从 Webpack 迁移到 JSPM。我们有简单的 App 组件。我正在阅读这篇文章 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) { }
}
和 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){
}
}
在 Webpack 中工作正常,但在 systemjs 中出现此错误
EXCEPTION: No provider for Http! (App -> Bus -> Http)
我读过
我也玩
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) { }
}
但错误更奇怪
EXCEPTION: No provider for ConnectionBackend! (App -> Http -> ConnectionBackend)
我什至尝试更改为 angular2-beta@6。还有 angular2-beta@1 你知道我做错了什么吗?谢谢
使用加载程序版本:systemjs@0.19.23
>=RC.5
将 HttpModule
添加到 AppModule
的导入中:
@NgModule({
imports: [HttpModule],
...
})
export class AppModule {}
<= RC.5
您需要将 HTTP_PROVIDERS
添加到 bootstrap
import {HTTP_PROVIDERS} from 'angular2/http';
<!-- -->
bootstrap(AppComponent, [HTTP_PROVIDERS]);
另见 https://angular.io/docs/ts/latest/api/http/HTTP_PROVIDERS-let.html
您需要在引导应用程序时定义 HTTP_PROVIDERS
提供程序:
import {HTTP_PROVIDERS} from 'angular2/http';
bootstrap(AppComponent, [ HTTP_PROVIDERS ]);
或者如果您想在组件级别指定它:
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) { }
}