在不同的页面

时间:2018-01-27 16:00:44

标签: angular typescript firebase ionic-framework

我正在尝试让用户在首次登录其帐户时转到个人资料设置页面。我正在使用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');
          }
        })
      });
    }
 }

个人资料-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');
         })
     })
   }

1 个答案:

答案 0 :(得分:1)

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