I have developed a User
class to be used as a singleton service in multiple components.
Could you please review if the Injectable()
declaration is correct?
import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
import { NavController, NavParams } from 'ionic-angular';
import { Facebook } from 'ionic-native';
import { HomePage } from '../../pages/home/home';
@Injectable()
export class User {
public email;
public pass;
private token;
private picture
private fbToken
constructor(public http : Http){}
public getEmail(){
return this.email;
}
// more getters and setters.
The functionality works well with the first component (LoginPage)!
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { RegisterPage } from '../register/register';
import { User } from '../../classes/singleton/user';
enum TpLogin {
defLogin,
fbLogin
}
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
public tpLogin = TpLogin;
constructor(public navCtrl: NavController, public navParams: NavParams, public user : User){}
ionViewDidLoad() {}
goHome(tpLogin : TpLogin){
var obj = this;
switch (tpLogin) {
case TpLogin.defLogin : {obj.user.login(this.navCtrl); break;}
case TpLogin.fbLogin : {obj.user.fbLogin(this.navCtrl); break;}
}
}
}
However, when I import the User
class into other components and run the application, an error occurs:
Runtime Error Can't resolve all parameters for HomePage: ([object Object], [object Object], ?).
Note:
I have imported the User
service as a provider in appModule.ts
, as shown below:
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { HttpModule } from '@angular/http';
import { MyApp } from './app.component';
import { LoginPage } from '../pages/Login/login';
import { RegisterPage } from '../pages/register/register';
import { HomePage } from '../pages/home/home';
import { InfoPage } from '../pages/info/info';
import { User } from '../classes/singleton/user';
@NgModule({
declarations: [
MyApp,
LoginPage,
RegisterPage,
HomePage,
InfoPage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
LoginPage,
RegisterPage,
HomePage,
InfoPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
User
]
})
export class AppModule {}
Any assistance would be appreciated.