我正在使用 AngularFire 和 Angular 8 来构建一个应用程序,但我有一个愚蠢的问题(我认为这实际上很愚蠢)。

我构建了一个简单的服务来包装 AngularFireAuth :

import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { User } from 'firebase';
import { Subject } from 'rxjs';
import { MessageService } from 'primeng/api';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  private user: Subject<User> = new Subject();
  private isLoggedIn: Subject<boolean> = new Subject();

  constructor(private afAuth: AngularFireAuth, private messageService: MessageService) {
    this.afAuth.auth.onAuthStateChanged(user => {
      this.user.next(user);
      this.isLoggedIn.next(user !== null);
    });
  }

  isAuthenticated() {
    return this.isLoggedIn.asObservable();
  }
}

然后,我将它注入(inject)到我的 HomeComponent 中并订阅了 Observable 方法返回的 isAuthenticated:

import { Component, OnInit } from "@angular/core"
import { AuthService } from '../auth/auth.service';

@Component({
  selector: 'app-homepage',
  styleUrls: ['./homepage.component.scss'],
  templateUrl: './homepage.component.html'
})
export class HomepageComponent implements OnInit {
  isAuthenticated: boolean = false;

  constructor(private authService: AuthService) { }

  ngOnInit() {
    this.authService.isAuthenticated().subscribe((isAuth) => {
      this.isAuthenticated = isAuth;
      console.log(`User is authenticated? ${this.isAuthenticated}`);
    });
  }
}

但是,当调用传递给 subscribe 方法的箭头函数时,不会执行重新渲染。但是,console.log 调用确实在 DevTools 上显示“用户已通过身份验证?true”。

我做过的其他一些测试:如果我从传递给 setTimeout 的箭头函数中调用 subscribe ,结果是一样的。无需重新渲染,DevTools 上的消息显示“用户已通过身份验证?真”。

但是,如果我在 setTimeout 之外调用 subscribe (在此测试中延迟 10 秒),则在这 10 秒后重新渲染组件:

import { Component, OnInit } from "@angular/core"
import { AuthService } from '../auth/auth.service';

@Component({
  selector: 'app-homepage',
  styleUrls: ['./homepage.component.scss'],
  templateUrl: './homepage.component.html'
})
export class HomepageComponent implements OnInit {
  isAuthenticated: boolean = false;

  constructor(private authService: AuthService) { }

  ngOnInit() {
    this.authService.isAuthenticated().subscribe((isAuth) => {
      this.isAuthenticated = isAuth;
      console.log(`User is authenticated? ${this.isAuthenticated}`);
    });

    setTimeout(() => {
      this.isAuthenticated = true;
      console.log(`User is authenticated? ${this.isAuthenticated}`);
    }, 10000)
  }
}

我在这里缺少什么?我误解了什么?

最佳答案

这是因为在组件初始化之后你正在调用你的身份验证
在构造函数中调用它它工作

import { Component, OnInit } from "@angular/core"
import { AuthService } from '../auth/auth.service';

@Component({
  selector: 'app-homepage',
  styleUrls: ['./homepage.component.scss'],
  templateUrl: './homepage.component.html'
})
export class HomepageComponent implements OnInit {
  isAuthenticated: boolean = false;

   constructor(private authService: AuthService) {
     this.authService.isAuthenticated().subscribe((isAuth) => {
      this.isAuthenticated = isAuth;
      console.log(`User is authenticated? ${this.isAuthenticated}`);
    });
    }

  ngOnInit(){}

}

关于javascript - AngularFire 不会触发更改检测,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59986113/

10-11 14:54