Angular 2 HTTP "Cannot resolve all parameters for 'AppService'"
Asked Answered
C

4

26

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

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.

Here are some code snippets:

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

}

I'm trying to call a controller on the server to eventually load a JSON file into a data table. Pretty straightforward stuff, but the way I'm loading the Http modules seems to be wrong. Any help will be much appreciated.

Coney answered 12/2, 2016 at 19:9 Comment(3)
are you using typescript compiler? if no then this would be the case https://mcmap.net/q/535593/-error-using-http-json-angularjs-2Anacoluthia
That fixed it... ThanksConey
This should work like this. But if you don't use TypeScript but only ES6 (no type support for method parameters), you need specify additional metadata to Angular2 regarding what to inject. In this case, the @Pankaj's describes exactly how to do that ;-)Tangential
A
53

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());
    }
}
Anacoluthia answered 12/2, 2016 at 20:1 Comment(3)
This saved my hours of time. Thanks.Sprawl
This worked!. could you tell me what is " transpiling" and how to use typescript compiler to do thatRetention
@DhananjayK rather than explaining in comments, I'd highly recommend to go through this articleAnacoluthia
M
25

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
  }
}
Mizzenmast answered 24/8, 2016 at 15:27 Comment(0)
B
0

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

Bulbar answered 13/7, 2017 at 20:9 Comment(0)
R
0

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';
Rasure answered 6/9, 2019 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.