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

前端 未结 4 1369
梦毁少年i
梦毁少年i 2020-12-10 01:27

I tried to import the http provider into a service, but I\'m getting the following error:

Cannot resolve all parameters for \'AppService\'(?). Make su

相关标签:
4条回答
  • 2020-12-10 01:41

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

    0 讨论(0)
  • 2020-12-10 01:48

    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
      }
    }
    
    0 讨论(0)
  • 2020-12-10 01:56

    Another possible solution is that you are missing some polyfills. Anyone who had no luck with another answers should try to add this polyfill:

    import 'core-js/es7/reflect';
    
    0 讨论(0)
  • 2020-12-10 02:07

    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());
        }
    }
    
    0 讨论(0)
提交回复
热议问题