【发布时间】:2016-02-02 20:30:07
【问题描述】:
当尝试在 App 组件(我们正在引导的组件)中使用 Http 时,一切正常:
export default class AppComponent {
constructor(public http: Http){
console.log(this.http);
}
getData() {
}
}
bootstrap(AppComponent, [HTTP_PROVIDERS]);
但如果我用 CustomHttpService 包装 Http(并且经常将 CustomHttpService 添加到引导组件数组):
custom-http-service.ts:
export default class CustomHttpService {
constructor(public http: Http){
console.log(this.http);
}
getData() {
}
}
app.ts:
bootstrap(AppComponent, [CustomehttpService]);
我明白了:
NoAnnotationError {message: "Cannot resolve all parameters for CustomHttpServic…ake sure they all have valid type or annotations.", stack: $
## _onError ##
TypeError: Cannot read property 'get' of undefined
at angular2.dev.js:19620
at Zone.run (angular2.dev.js:138)
at Zone.run (angular2.dev.js:10644)
at NgZone.run (angular2.dev.js:10607)
at ApplicationRef_.bootstrap (angular2.dev.js:19615)
at Object.commonBootstrap (angular2.dev.js:26650)
at Object.bootstrap (angular2.dev.js:27475)
at Object.<anonymous> (app.ts:23)
at app.ts:23
at SystemJSLoader.__exec (system.src.js:1384)
Uncaught TypeError: Cannot read property 'get' of undefined
我错过了什么?在 bootstrap 函数中导入模块和注册自定义服务还不够吗?
这个问题与我们想要注入应用程序的每个没有元数据(组件、视图等)的类相关。
【问题讨论】:
-
你应该阅读这个article,准确解释你所看到的。
-
@EricMartinez。这是完美的!
标签: angular