ionViewDidLoad()方法不起作用?

时间:2018-04-11 22:04:44

标签: angular email authentication ionic-framework ionic3

我正在尝试实施电子邮件身份验证一切正常,但Methode ionViewDidLoad无法识别?我尝试了Methode ionViewWillLoad(),但也没有用。

有人知道可能是什么问题吗?

我的代码: home.ts

    import {Component} from '@angular/core';
    import {NavController, IonicPage, NavParams, ToastController} from 'ionic-angular';
    import { AngularFireAuth} from 'angularfire2/auth';
    @IonicPage()
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {

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



      ionViewDidLoad(){
        this.afAuth.authState.subscribe(data => {
          if (data.email && data.uid) {
            this.toast.create({
              message: `Welcome to APP_NAME, ${data.email}`,
              duration: 5000
            }).present();
          }
        })
      }
    }

1 个答案:

答案 0 :(得分:1)

您如何调用HomePage组件?当您从NavController推送/弹出实现它的组件时,会调用ionViewDidLoad()钩子,如文档中所述:https://ionicframework.com/docs/api/navigation/NavController/

如果组件的调用不是由NavController调用的,则可能不会调用该钩子。那么你可以使用构造函数来调用你的函数。

import {Component} from '@angular/core';
import {NavController, IonicPage, NavParams, ToastController} from 'ionic-angular';
import { AngularFireAuth} from 'angularfire2/auth';
@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(private afAuth: AngularFireAuth, 
              private toast: ToastController, 
              public navCtrl: NavController, 
              public navParams: NavParams) 
   {
     this.afAuth.authState.subscribe(data => {
      if (data.email && data.uid) {
        this.toast.create({
          message: `Welcome to APP_NAME, ${data.email}`,
          duration: 5000
        }).present();
      }
    })
  }

}