Angular 2 HTTP “Cannot resolve all parameters for 'AppService'”

谁说胖子不能爱 提交于 2019-11-28 09:38:59

Seems like you are not using typescript compiler for transpiling files to js, In that case you need to have use @Inject while injecting any dependency inside a component constructor.

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());
    }
}
Yariv Katz

Another way to do it, which saves some typing in the future would be:

in your tsconfig.json add compilerOptions.emitDecoratorMetadata=true

example of simple tsconfig.json would be:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}
Leonardo López

I had the same issue, and for me the problem was that i was missing import { HttpModule } from '@angular/http' in app.module.ts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!