23

我试图HTTP提供导入服务,但我发现了以下错误:角2 HTTP“无法解析所有的参数AppService服务'”

Cannot resolve all parameters for 'AppService'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AppService' is decorated with Injectable.

这里有一些代码段:

<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)); 

boot.ts

import {bootstrap} from 'angular2/platform/browser' 
import {HTTP_PROVIDERS} from 'angular2/http' 
import 'rxjs/add/operator/map' 
import {AppComponent} from 'RCO/Areas/ViewOrganization/AngularTemplates/app.component' 
import {AppService} from 'RCO/Areas/ViewOrganization/AngularTemplates/app.service' 

bootstrap(AppComponent, [HTTP_PROVIDERS, AppService]); 

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模块的方式似乎是错误的。任何帮助都感激不尽。

+0

你使用的是打字稿编译器吗?如果没有,那么这将是这种情况http://stackoverflow.com/a/35350172/2435473 –

+0

修复它......谢谢 – ddpdoj

+1

这应该是这样的。但是,如果您不使用TypeScript,而只使用ES6(对于方法参数不支持类型),则需要为Angular2指定有关注入内容的附加元数据。在这种情况下,@ Pankaj的描述了如何做到这一点;-) –

回答

49

好像你没有使用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()); 
    } 
} 
+1

这节省了我的时间。谢谢。 –

+0

这工作!你能告诉我什么是“transpiling”,以及如何使用typecript编译器来做到这一点 – dsk

+1

@DhananjayK而不是在评论中解释,我强烈建议通过[本文](https://scotch.io/tutorials/) javascript-transpilers-what-they-are-why-we-need-them) –

23

另一种方式来做到这一点,从而节省了在未来一段打字是:

tsconfig.json添加compilerOptions.emitDecoratorMetadata=true

例如简单的tsconfig.json是:

{ 
    "compilerOptions": { 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true 
    } 
} 
+4

这节省了我的一天! –

0

我有同样的问题,对我来说,问题是我在app.module.ts

相关问题