离子吐司消息无法读取属性'创建'未定义的

时间:2018-01-09 20:38:35

标签: android angular firebase ionic-framework firebase-authentication

我尝试在用户登录失败时尝试创建Toast消息但是toast消息在create属性上给出了错误。我正在使用firebase身份验证。

  

无法读取属性'创建'未定义的

login.ts

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

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

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

  user = {} as User

  constructor(private afAuth: AngularFireAuth,
    public navCtrl: NavController, public navParams: NavParams) {
  }

  async login(user:User){
    this.afAuth.auth.signInWithEmailAndPassword(user.email,user.password)
    .then(res => {
      this.navCtrl.setRoot('TabsPage');
    }, err => {
      switch (err.code) { 
        case "auth/wrong-password":
          this.toast.create({
            message: `Wrong Password`,
            duration: 3000
          }).present();
        break;
      }
    });
  }

}

1 个答案:

答案 0 :(得分:2)

您需要在构造函数中初始化ToastController并引用它。

例如:

constructor(
    private afAuth: AngularFireAuth,
    public navCtrl: NavController,
    public navParams: NavParams,
    public toastCtrl: ToastController /* This bit */
) {}

然后......

let toast = this.toastCtrl.create({
    message: 'Wrong password, holmes.',
    duration: 3000,
    position: 'top'
});
toast.present();