Javascript 当用户在爱奥尼亚3上的密码或电子邮件出错时,如何提醒用户?
我正在尝试使用showAlert创建一个检查,以便在用户输入错误的电子邮件或密码时通知用户,但我无法使用try and catch 我能不能用try-and-catch来做这个 下面是我的login.ts代码示例Javascript 当用户在爱奥尼亚3上的密码或电子邮件出错时,如何提醒用户?,javascript,typescript,ionic3,Javascript,Typescript,Ionic3,我正在尝试使用showAlert创建一个检查,以便在用户输入错误的电子邮件或密码时通知用户,但我无法使用try and catch 我能不能用try-and-catch来做这个 下面是我的login.ts代码示例 import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import { User } from '../../mod
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');
}
}
我决定如下
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');
}
}
你目前遇到了什么问题?如果我的问题不是很清楚,很抱歉,但我希望用户在输入错误的用户或密码时得到提醒。我的意思是,你发布的代码出了什么问题?我的函数在Catch中不起作用。你不应该尝试在另一个方法中定义一个方法。如果只想在catch块中显示警报,请删除
showarert(){
和匹配的}
,保留其中的代码。如果还想从其他地方调用此方法,请将其移到login
之外,并将其称为this.showart()代码>。