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

相關問題