离子2简单单身人士服务[部分错误]

时间:2017-08-08 21:25:23

标签: javascript angular typescript ionic2

我创建了类User以在几个组件中用作服务(单例)。 你能否检查我是否正确地写了Injectable()

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.

在第一个组件(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;}
        }              
    }        
}

但是当我在其他组件中导入User并运行应用程序时,我收到此错误:

  

运行时错误   无法解析HomePage的所有参数:([object Object],[object Object],?)。

PS .. 我在User中将服务appModule.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 { 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 {}

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是我的HOME.TS



import { User } from '../../classes/singleton/user';
import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import { InfoPage } from '../info/info';
import {Http, Headers} from '@angular/http';


declare var google;

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
    public listaBarber = [];    
    @ViewChild('map') mapElement: ElementRef;
     map: any;

  constructor(public navCtrl: NavController, public http: Http, public user : User) {      
        var url = 'https://cors-anywhere.herokuapp.com/http://98.111.45.209/barbearias';
        var headers = new Headers();          
        
        headers.append('Content-Type', 'application/json');           
        //headers.append('x-access-token', this.user.getToken());
        
        http.get(url,{headers: headers}).subscribe(res => {console.log(res.json().data);
                                                           this.listaBarber = res.json().data},
                                                   (err) => {console.log(err._body);});
  };

  ionViewDidLoad(){
    this.loadMap();
  };

  loadMap(){
 
    let latLng = new google.maps.LatLng(-26.334302,-48.8524027);
 
    let mapOptions = {
      center: latLng,
      zoom: 15,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
 
    this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
 
  }    

    getInfo(id,entidade){    
        this.navCtrl.push(InfoPage,{allValues:{id:id,entidade:entidade}});        
    };
}