当ionic 3上的密码或电子邮件错误时,如何提醒用户?

时间:2018-08-20 02:43:38

标签: javascript typescript ionic3

我正在尝试使用showAlert创建一个支票,以在用户输入错误的电子邮件或密码时通知用户,但我无法使用try and catch

我是否可以尝试并抓住?

这是我的login.ts代码的示例

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { User } from '../../models/user';
import { AngularFireAuth } from "angularfire2/auth";
import { MenuController } from "ionic-angular";
import { AlertController } from "ionic-angular";

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  user = {} as User;

  constructor(private afAuth: AngularFireAuth, public navCtrl: NavController, public navParams: NavParams, public menu: MenuController, public alertCtrl: AlertController) {
  }

ionViewWillEnter(){
  this.menu.enable(false);
}

 async login(user: User){
  try{
    const result = await this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password);
    if (result) {
      this.navCtrl.setRoot('HomePage');
    }
  }
  catch {
    showAlert() {
      const alert = this.alertCtrl.create({
        title: 'New Friend!',
        subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
        buttons: ['OK']
      });
      alert.present();
    }
  }

 }

 register(){
   this.navCtrl.push('RegisterPage');
 }

}

1 个答案:

答案 0 :(得分:0)

我决定如下

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { User } from '../../models/user';
import { AngularFireAuth } from "angularfire2/auth";
import { MenuController } from "ionic-angular";
import { AlertController } from "ionic-angular";

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  user = {} as User;

  constructor(private afAuth: AngularFireAuth, public navCtrl: NavController, public navParams: NavParams, public menu: MenuController, public alertCtrl: AlertController) {

  }

  showAlert() {
    const alert = this.alertCtrl.create({
      title: 'Atenção',
      subTitle: 'Seu e-mail ou senha estão incorretos, por favor tente novamente.',
      buttons: ['OK']
    });
    alert.present();
  }

  ionViewWillEnter(){
    this.menu.enable(false);
  }

 async login(user: User){
  try{
    const result = await this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password);
    if (result) {
      this.navCtrl.setRoot('HomePage');
    }
  }
  catch {
    this.showAlert();
  }

 }

 register(){
   this.navCtrl.push('RegisterPage');
 }

}