Search code examples
typescriptionic2angular2-servicesionic2-providers

ionic2 provider "cannot load module"


I'm trying to add a provider to my application,but getting this error

Uncaught Error: Cannot find module "../providers/login/loginservice"

Project structure

this is my project structure

Here is how the app.module.ts looks

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 { HomePage } from '../pages/home/home';
import { Loginpage } from '../pages/loginpage/loginpage';

import { Loginservice } from '../providers/login/loginservice';


@NgModule({
  declarations: [
    MyApp,
    HomePage,
    Loginpage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    Loginpage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    Loginservice,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

loginpage.ts where i'm trying to use the provider

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Loginservice } from '../../providers/login/loginservice';

/**
 * Generated class for the Loginpage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-loginpage',
  templateUrl: 'loginpage.html'
  })
export class Loginpage {

  constructor(public navCtrl: NavController, public navParams: NavParams,public loginservice : Loginservice) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad Loginpage');
  }
  login(){
    console.log("inside Login");
    //this.loginservice.doLogin();
  }

}

*Update:Adding the loginservice code.

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

/*
  Generated class for the Loginservice provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class Loginservice {

  constructor(public http: Http) {
    console.log('Hello Loginservice Provider');
  }

}

I'm not able to find any solution to this. I tried changing the paths and removing and adding the provider again. as of now the provider doesn't have any functionality as i'm j ust trying ti successfully import it.

Error Screenshot

enter image description here

Please guide me.

Thanks Shruti Nair


Solution

  • Looking at the code you provided in the link above

    In the src/providers directory you have the loginservice.ts file but in loginpage.ts your import is

    import { Loginservice } from '../../providers/login/loginservice';
    

    There is no login directory, so the import should be

    import { Loginservice } from '../../providers/loginservice';
    

    Kindly change it in the app.module.ts file as well

    import { Loginservice } from '../providers/loginservice';
    

    Hope this helps you, Thanks.