【问题标题】:Logout button not function注销按钮不起作用
【发布时间】:2020-02-01 03:19:36
【问题描述】:

所以我使用标签页上的注销按钮功能导航回登录页。不幸的是,当用户已经登录并注册时,我无法单击“退出”按钮

我已经尝试过身份验证功能来导航回登录

tabs.page.ts

import {Component, OnInit, ViewChild} from '@angular/core';
import {UserService} from '../user.service';
import {AngularFireAuth} from '@angular/fire/auth';
import { Router} from '@angular/router'
import {AngularFirestore} from '@angular/fire/firestore'

@Component({
selector: 'app-tabs',
templateUrl: 'tabs.page.html',
styleUrls: ['tabs.page.scss'],
})
export class  TabsPage implements OnInit {
constructor(
public afAuth: AngularFireAuth,
public router: Router,
public afstore: AngularFirestore
 ) {}

ngOnInit()  {
}
 async logout() {
 try { 
 this.afAuth.auth.signOut();
this.router.navigate(['/loginPage']);
} catch (error) {
 console.log('Error', error);
}
}
}

tabs.page.html

 <ion-item>
 <ion-button ion-button color="danger"   (click)="logout()" full>signOut
</ion-button>
</ion-item>

【问题讨论】:

    标签: typescript firebase ionic4


    【解决方案1】:

    你需要把awaitasync放在那里。

    async logout() {
      try { 
        // add await here:
        await this.afAuth.auth.signOut();
        this.router.navigate(['/loginPage']);
      } catch (error) {
        console.log('Error', error);
      }
    }
    

    或者您也可以使用.then() 的写作风格:

    return this.afAuth.auth.signOut().then(() => {
      this.router.navigate(['/loginPage']);
    }).error((error) => {
      console.log('Error', error);
    });
    

    否则路由器会在signOut()完成运行之前重定向到下一页。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 2014-08-04
      • 2020-07-21
      相关资源
      最近更新 更多