ish*_*007 9 observable firebase-realtime-database angular
在服务中声明如下:
public currentDBUserBS$: any;
constructor(private afDb: AngularFireDatabase){}
fetchUser(uid){
this.afDb.object(`users/${uid}`).valueChanges().subscribe((dUser) => {
if (dUser) {
this.currentDBUserBS$ = dUser;
}
});
}
Run Code Online (Sandbox Code Playgroud)
现在尝试在模板中使用
template: `<li *ngIf="(authService.currentDBUserBS$ | async)?.role=='admin'"> Something </li>`
Run Code Online (Sandbox Code Playgroud)
错误堆栈:
ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'
at invalidPipeArgumentError (common.js:4219)
at AsyncPipe._selectStrategy (common.js:5630)
at AsyncPipe._subscribe (common.js:5612)
at AsyncPipe.transform (common.js:5586)
at Object.View_AppNavigatorComponent_0._co [as updateDirectives] (AppNavigatorComponent.html:40)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:14339)
at checkAndUpdateView (core.js:13508)
at callViewAction (core.js:13858)
at execComponentViewsAction (core.js:13790)
at checkAndUpdateView (core.js:13514)
Run Code Online (Sandbox Code Playgroud)
tom*_*cht 17
您正在currentDBUserBS$从Firebase获取对象,而不是Observable.如果您想这样做,则必须删除| async模板中的内容.
但是,您可以执行以下操作:
fetchUser(uid){
this.currentDBUserBS$ = this.afDb.object(`users/${uid}`).valueChanges();
}
Run Code Online (Sandbox Code Playgroud)
.valueChanges() 为您提供可观察的,并在发出新值或修改现有值时自动更新.
| 归档时间: |
|
| 查看次数: |
25502 次 |
| 最近记录: |