我試圖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模塊的方式似乎是錯誤的。任何幫助都感激不盡。
你使用的是打字稿編譯器嗎?如果沒有,那麼這將是這種情況http://stackoverflow.com/a/35350172/2435473 –
修復它......謝謝 – ddpdoj
這應該是這樣的。但是,如果您不使用TypeScript,而只使用ES6(對於方法參數不支持類型),則需要爲Angular2指定有關注入內容的附加元數據。在這種情況下,@ Pankaj的描述瞭如何做到這一點;-) –