【问题标题】:Ionic Angular if statement being called on different page在不同页面上调用 Ionic Angular if 语句
【发布时间】:2018-01-27 16:00:43
【问题描述】:

我试图让用户在首次登录其帐户时转到个人资料设置页面。我正在使用 if 语句来执行此操作,该语句检查用户的 id 是否在 firebase 数据库中以及是否与配置文件相关联。如果没有,用户将被带到个人资料制作页面,那么一旦他们制作了个人资料,他们就会被发送到他们的个人资料,如果他们有个人资料,他们会被直接带到他们的个人资料。

问题是,一旦他们制作了个人资料,即使在登录页面关闭后,个人资料页面仍会从登录页面中的 if 语句加载。为什么会这样?

login.ts

login(user: User){
 this.afAuth.auth.signInWithEmailAndPassword(user.email,user.password)
 .then(res => {
   //check if user has made profile if not send to profile setup page
    let user = firebase.auth().currentUser;
    if(user.emailVerified){
      this.afAuth.authState.take(1).subscribe(data => {
        let profileCollection = this.afs.collection('profiles').doc(`${data.uid}`).valueChanges();
        profileCollection.subscribe(userProfile => {
          if (userProfile == null){
            console.log(userProfile);
            this.navCtrl.setRoot('ProfileSetupPage');
          }else{
            console.log('LoginPage Load Tabs Page');
            this.navCtrl.setRoot('ProfilePage');
          }
        })
      });
    }
 }

profile-setup.ts

 createProfile(){
   if(this.profileForm.valid){
     this.afAuth.authState.take(1).subscribe(auth => {
       this.afs.collection('profiles').doc(`${auth.uid}`).set(this.profile)
         .then(res => {
           this.navCtrl.setRoot('ProfilePage');
         })
     })
   }

【问题讨论】:

    标签: angular typescript firebase ionic-framework


    【解决方案1】:

    我相信登录的订阅仍然处于活动状态并接受事件,因此您需要在重定向时在 login.ts 中取消订阅,或者在初始化时在 profile-setup.ts 中取消订阅。

    【讨论】:

    • 哦,我明白了,因为它仍在从订阅中获取数据。所以要解决这个问题,我会添加 this.profileCollection.unsubscribe(); this.navCtrl.setRoot('ProfileSetupPage'); 之后?
    • this.afAuth.authState.take(1).unsubscribe();我对 Ionic Angular 不是很熟悉,但我相信这持有订阅并需要取消订阅
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2017-02-08
    • 1970-01-01
    • 2015-01-17
    • 2020-12-27
    • 2011-03-05
    相关资源
    最近更新 更多