我正在尝试使用 firebase 访问 angular2 中的 auth 对象,但我感到困惑。我可以在控制台中看到用户身份验证凭据,但身份验证变量似乎为空。
app.component.ts
import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
import { AuthProviders, AuthMethods } from 'angularfire2';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
constructor(public af: AngularFire) {
this.af.auth.subscribe(auth => console.log(auth)); //nothing in the console
console.log(this.af.auth); // shows AngularFireAuth object with user credentials but says it is private
}
login() {
console.log("logging in...");
this.af.auth.login({
provider: AuthProviders.Google,
method: AuthMethods.Popup,
});
}
}
app.component.html
<div *ngIf="auth">You are logged in</div>
<div *ngIf="!auth">Please log in</div>
auth
即使显示用户凭据,该变量也似乎为空console.log(this.af.auth);
,因此仅Please log in
显示在 html 中。