ionic2提供者“无法加载模块”

时间:2017-04-26 05:57:33

标签: typescript ionic2 angular2-services ionic2-providers

我正在尝试向我的应用添加提供程序,但收到此错误

未捕获错误:找不到模块“../ providers / login / loginservice”

项目结构

this is my project structure

以下是app.module.ts的外观

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我正在尝试使用提供商

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

}

*更新:添加登录服务代码。

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

}

我无法找到任何解决方案。 我尝试更改路径并再次删除和添加提供程序。 截至目前,提供商没有任何功能,因为我正在尝试成功导入它。

错误截图

enter image description here

请指导我。

由于 Shruti Nair

1 个答案:

答案 0 :(得分:0)

查看您在上面链接中提供的代码

在src / providers目录中,您有loginservice.ts文件,但在loginpage.ts中,您的导入是

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

没有登录目录,因此导入应为

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

请在app.module.ts文件中更改它

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

希望这对你有所帮助,谢谢。