android - Ionic Toast 消息无法读取未定义的属性 'create'

标签 android angular firebase ionic-framework firebase-authentication

当用户登录失败时,我尝试创建一条 toast 消息,但 toast 消息在创建属性上给我一个错误。我正在使用 firebase 身份验证。

Cannot read property 'create' of undefined

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

}

最佳答案

您需要在构造函数中初始化 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();

关于android - Ionic Toast 消息无法读取未定义的属性 'create',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176311/

相关文章:

android - 由于使用权限 maxSdkVersion,Google Play APK 上传失败

android - 如何使用命令在android中执行dex文件?

java - 如何在 Firestore Android 中的事务中增加计数器?

javascript - 检索 Firebase 对象 ID

android - 如何刷新或更新 viewpager android 中的所有 fragment

Android Studio 未处理的异常警告

android - 具有服务器端组件的 Android 应用程序是否应该直接访问 Facebook?

node.js - Angular,未找到图像(GET 404),但运行 ng build 后显示图像

validation - Angular 2 : how to apply two or more validators to the same control?

angular - 当我尝试构建 ionic 应用程序时,构建gradle存在问题