可见未刷新视图-Angular 2+

时间:2018-12-03 01:24:51

标签: angular rxjs

显示Facebook弹出窗口后,当我从login.ts组件在auth.service.ts中调用login()logout()时,在主视图中isAuth仍然为false,但是如果我执行{ console.log(this.isAuth)中的{1}}可以在控制台中正确更新。

当我不使用Firebase Facebook登录名而直接致电subscribe()login()时,工作正常

首页

HTML

logout()

TS

 <h1> {{isAuth}} </h1>

AuthService

 isAuth = false;

 constructor(private _auth:AuthService) {
    _auth.authState.subscribe(state => this.isAuth = state)
   }

主题是从

导入的
  

从“ rxjs / Subject”导入{Subject};

login.ts

authState = new Subject<boolean>();

login(access_token) {
    this.authState.next(true);
  }

 logout() {
    this.authState.next(false);
  }

facebook promise得以解决,signInWithFacebook() { this.afAuth.auth .signInWithPopup(new firebase.auth.FacebookAuthProvider()) .then((res: any) => { const access_token = res.credential.accessToken; this._auth.login(access_token); this._router.navigateByUrl('/home'); }); 被调用,this._auth.login()被更改,但是视图未更新...

1 个答案:

答案 0 :(得分:1)

也许您可以尝试NgZone

ts

isAuth = false;

constructor(private _auth:AuthService, private ngZone: NgZone) {
ngZone.run(() => {
   _auth.authState.subscribe(state => this.isAuth = state)
  });
}