【发布时间】:2020-02-02 15:29:37
【问题描述】:
我是 Angular 的临时用户,所以也许我遗漏了一些明显的东西。
我跟随 fireship.io 的 this lesson 使用 angularfire 在我的 Angular 应用程序中集成身份验证。
登录后,可观察的 AuthService.user$ 发生变化,但 UserProfile 组件中的模板没有更新。
使用下面的代码 sn-p 确认数据在那里。
<pre>{{auth.user$ | async | json}}</pre>
这似乎与在 ngzone 之外更新的 observable 有关。 我试图通过在组件中注入 ChangeDetectorRef 并从 AuthService.user$ 的订阅回调中触发 detectChange 来手动检测更改,但没有成功。
我只是通过将 user-profile.component.ts 更改为以下内容来使其按预期工作:
@Component({
selector: 'app-user-profile',
templateUrl: './user-profile.component.html',
styleUrls: ['./user-profile.component.css']
})
export class UserProfileComponent implements OnInit {
userData?: User;
constructor(public auth: AuthService) {
this.auth.user$.subscribe(d => this.userData = d)
}
}
并将user-profile.component.html改为如下:
<div *ngIf="userData; then authenticated else guest">
</div>
<ng-template #guest>
<h3>Hello</h3>
<p>Login to get started...</p>
<button (click)="auth.googleLogin()">
<i class="fa fa-google"></i> Connect Google
</button>
</ng-template>
<ng-template #authenticated>
<div *ngIf="userData as user">
<h3>Hello, {{ user.displayName }}</h3>
<img [src]="user.photoURL">
<button (click)="auth.signOut()">Logout</button>
</div>
</ng-template>
这是我的依赖项,从 package.json 中提取。
{
"@angular/animations": "~8.2.14",
"@angular/common": "~8.2.14",
"@angular/compiler": "~8.2.14",
"@angular/core": "~8.2.14",
"@angular/fire": "^5.4.0",
"@angular/forms": "~8.2.14",
"@angular/platform-browser": "~8.2.14",
"@angular/platform-browser-dynamic": "~8.2.14",
"@angular/router": "~8.2.14",
"firebase": "^7.8.0",
"rxjs": "~6.4.0",
"tslib": "^1.10.0",
"zone.js": "~0.9.1"
}
知道我可能缺少什么吗?
【问题讨论】:
-
你能在 stackblitz 上分享你的代码吗?
-
它无法按原样运行,因为您需要在环境中提供有效的 firebase 配置。
标签: angular firebase google-cloud-platform angularfire