【发布时间】:2016-10-06 16:07:10
【问题描述】:
我正在尝试使用 Angular 2 Http 服务,但我得到了
无法解析 AppComponent 的所有参数:(?)
哪个有这个签名
constructor(public http:Http){ }
我将问题追踪到声明 Http 的提供程序。
In the angular 2 quickstart他们说
在这个演示中,我们通过将其他 NgModule 导入到我们的根 NgModule 来注册提供程序。
但我已经有了
@NgModule({
imports: [ BrowserModule, HttpModule, FormsModule ],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
所以,我知道提供商应该没问题。
在文件 http.d.ts 的代码文档中,我发现:
* import {Http, HTTP_PROVIDERS} from '@angular/http';
但是当我尝试写这篇文章时,我发现HTTP_PROVIDERS 不存在(我猜是在 GA 之前)
解决此错误的唯一方法是在签名中添加@Inject(Http)。
如果不在签名中添加@Inject(Http),如何使其工作?
这是我的 systemjs 配置
<script>
System.config({
transpiler: 'typescript',
map: {
'@angular/core': './node_modules/@angular/core/bundles/core.umd.js',
'@angular/platform-browser-dynamic': './node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/platform-browser': './node_modules/@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/compiler': './node_modules/@angular/compiler/bundles/compiler.umd.js',
'@angular/common': './node_modules/@angular/common/bundles/common.umd.js',
'@angular/http': './node_modules/@angular/http/bundles/http.umd.js',
'@angular/forms': './node_modules/@angular/forms/bundles/forms.umd.js',
'rxjs': './node_modules/rxjs'
},
packages: {
'./app': {
defaultExtension: 'ts'
},
'@angular/http':{
defaultExtension:'ts'
},
'rxjs': {
defaultExtension: 'js'
}
}
})
System.import('./app/main.ts').catch(function (err) {
console.error(err);
});
</script>
这是我的 tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
}
你可以在https://github.com/GuyMograbi/angular2-simple-setup看到我所有的代码
【问题讨论】:
-
你能添加你的
tsconfig.json吗? -
@yurzui 添加了
tsconfig.json和 systemjs 配置以防万一。
标签: angular