Typescript 角度2 HTTP“;无法解析';应用服务&x27&引用;
我试图将http提供程序导入服务,但出现以下错误: 无法解析“AppService”(?)的所有参数。确保所有参数都用Injector修饰或具有有效的类型批注,并且“AppService”用Injectable修饰 以下是一些代码片段:Typescript 角度2 HTTP“;无法解析';应用服务&x27&引用;,typescript,angular,systemjs,angular2-services,angular2-injection,Typescript,Angular,Systemjs,Angular2 Services,Angular2 Injection,我试图将http提供程序导入服务,但出现以下错误: 无法解析“AppService”(?)的所有参数。确保所有参数都用Injector修饰或具有有效的类型批注,并且“AppService”用Injectable修饰 以下是一些代码片段: <script src="~/es6-shim/es6-shim.min.js"></script> <script src="~/systemjs/dist/system-polyfills.js"></script&
<script src="~/es6-shim/es6-shim.min.js"></script>
<script src="~/systemjs/dist/system-polyfills.js"></script>
<script src="~/angular2/bundles/angular2-polyfills.js"></script>
<script src="~/systemjs/dist/system.src.js"></script>
<script src="~/rxjs/bundles/Rx.js"></script>
<script src="~/angular2/bundles/angular2.dev.js"></script>
<script src="~/angular2/bundles/http.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
map: { 'rxjs': 'RCO/rxjs' },
packages: {
RCO: {
format: 'register',
defaultExtension: 'js'
},
'rxjs': {defaultExtension: 'js'}
}
});
System.import('RCO/Areas/ViewOrganization/AngularTemplates/boot')
.then(null, console.error.bind(console));
app.service.ts
import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Rx';
@Injectable()
export class AppService {
constructor(private http: Http) { }
// Uses http.get() to load a single JSON file
getTableData() {
return this.http.get('...').map((res: Response) => res.json());
}
}
我试图调用服务器上的控制器,最终将JSON文件加载到数据表中。非常简单的东西,但我加载Http模块的方式似乎是错误的。任何帮助都将不胜感激。似乎您没有使用
typescript
编译器将文件传输到js
,在这种情况下,您需要在组件构造函数中注入任何依赖项时使用@Inject
import {Injectable, Inject} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Rx';
@Injectable()
export class AppService {
constructor(@Inject(Http) private http: Http) { }
// Uses http.get() to load a single JSON file
getTableData() {
return this.http.get('...').map((res: Response) => res.json());
}
}
另一种方法是:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
在您的tsconfig.json
addcompilerOptions.emitdecorformatadata=true
简单的tsconfig.json
示例如下:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
我也有同样的问题,对我来说,问题是我在
app.module.ts
中的“@angular/http”中缺少import{HttpModule},另一个可能的解决方案是缺少一些多边形填充。任何没有其他答案的人都应尝试添加以下polyfill:
import 'core-js/es7/reflect';
您正在使用typescript编译器吗?如果没有,那么这将是修复它的情况。。。谢谢这应该是这样的。但如果您不使用TypeScript,而只使用ES6(不支持方法参数的类型),则需要为Angular2指定有关注入内容的额外元数据。在本例中,@Pankaj准确地描述了如何做到这一点;-)这节省了我几个小时的时间。谢谢,这很有效!。您能告诉我什么是“transpiling”以及如何使用typescript编译器吗that@DhananjayK与其在评论中解释,我强烈建议大家通过这篇文章来拯救我的一天!