I have problems with injecting service. The scenario is as follows:
App.ts
---- User.ts (service)
…|
…|- localDataBase.ts (service))
…|- DataServer.ts (service with http in constructor)
import {Injectable, Inject} from ‘@angular/core’;
import {LocalDataBase} from ‘./database’;
import {Platform} from ‘ionic-angular’;
import {DataServer} from ‘./dataServer’;
import {Http, Response, Headers, RequestOptions} from ‘@angular/http’;
import {Injectable} from ‘@angular/core’;
import {Http, Response, Headers, RequestOptions} from ‘@angular/http’;
import {AppSettings} from ‘./appSettings’;
import {Observable} from ‘rxjs/Observable’;
import ‘…/import/rxjs-operators’;
@Injectable()
export class DataServer {
constructor(private http: Http) {
When i launch app in chrome, i have the following error in console:
reflective_provider.js:232
Uncaught Cannot resolve all parameters for ‘User’(LocalDataBase, Platform, undefined). Make sure that all the parameters are decorated with Inject or have valid type annotations and that ‘User’ is decorated with Injectable.
First of all, you don’t need to declare HTTP_PROVIDERS in your app’s providers array. Ionic does that for you behind the scenes.
Did you change the way the code is structured in compilation units (I know that’s not really a thing in JavaScript, but I come from a C background and don’t know what else to call it) for the purposes of this post? Is it possible that you’re running into the problem described in this post about forward references and DI?
I have reproduced the problem with a basic project.
ionic start Test tutorial --v2 --ts
added services/dataserver.ts
import {Injectable, Inject, forwardRef} from ‘@angular/core’;
import {Observable} from ‘rxjs/Observable’;
@Injectable()
export class DataServer {
constructor() {
}
}
added services/user.ts (with and without @Inject(forwardRef(() => DataServer)) )
import {Injectable, Inject, forwardRef} from ‘@angular/core’;
import {DataServer} from ‘./dataServer’;
@Injectable()
export class User {
public id = null;
public language = null;
constructor(private dataServer: DataServer) {
}
}
added provider in app.ts:
providers: [DataServer, User]
Then i launch “ionic serve”.
And i can see in chrome the following error:
reflective_provider.js:232
Uncaught Cannot resolve all parameters for ‘User’(undefined). Make sure that all the parameters are decorated with Inject or have valid type annotations and that ‘User’ is decorated with Injectable.
This is precisely why I hate some of TypeScript’s defaults - specifically emit on error and implicity any. If you set noEmitOnError to true, I believe this error would have been fatal.
What is really amazing (That was my case)
app.ts defined service without uppercase, typescript dont warm, javascript don’t warm
import {DataServer} from ‘./services/dataserver’; (
With a path like that,it seems not to detect the problem. This lead to a javascript variable without uppercase, and to my problem with dependency injection, DI trying to inject another variable with uppercase from user.ts.