为什么我必须在 FirebaseObjectObservable 上使用 $value
Why do I have to use $value on a FirebaseObjectObservable
我的 Firebase 数据库中有以下节点:
users
-$user
--public
---avatarUrl: 'https://myurl'
我这样检索 url:
lastMessage$: Observable<any>;
recipientAvatarUrl$: FirebaseObjectObservable<any>;
constructor(
public navCtrl: NavController,
public messageService: MessageService,
public authService: AuthService,
public userService: UserService
) {
console.log('ChatPreviewComponent#constructor');
}
ngOnInit(): void {
this.recipientAvatarUrl$ = this.userService.getUserAvatarUrl(this.chat.recipient, this.authService.getCurrenUserCompany());
}
函数"getUserAvatarUrl"在我的服务中如下:
getUserAvatarUrl(uid: string, company: string): any {
console.log('UserService#getUserAvatarUrl - Get user avatar url for ', uid);
let path = `/${company}/users/${uid}/public/avatarUrl`;
return this.af.database.object(path);
}
最后,这是我渲染图像的视图:
<ion-item (click)="openChat(shortChat.id)">
<ion-avatar item-left>
<img [src]="(recipientAvatarUrl$ | async)?.$value" />
</ion-avatar>
</ion-item>
我不明白为什么要用$value来获取节点的值。我认为异步管道会根据 AngularFire2 文档解包 FirebaseObjectObservable。
有线索吗?
如果您要订阅的 FirebaseObjectObservable 节点包含对象,则可以像在 JavaScript 对象中一样访问字段。
<img [src]="(recipientAvatarUrl$ | async)?.name" />
原始值(数字、字符串或布尔值)存储在 $value 下。
<img [src]="(recipientAvatarUrl$ | async)?.$value" />
我的 Firebase 数据库中有以下节点:
users
-$user
--public
---avatarUrl: 'https://myurl'
我这样检索 url:
lastMessage$: Observable<any>;
recipientAvatarUrl$: FirebaseObjectObservable<any>;
constructor(
public navCtrl: NavController,
public messageService: MessageService,
public authService: AuthService,
public userService: UserService
) {
console.log('ChatPreviewComponent#constructor');
}
ngOnInit(): void {
this.recipientAvatarUrl$ = this.userService.getUserAvatarUrl(this.chat.recipient, this.authService.getCurrenUserCompany());
}
函数"getUserAvatarUrl"在我的服务中如下:
getUserAvatarUrl(uid: string, company: string): any {
console.log('UserService#getUserAvatarUrl - Get user avatar url for ', uid);
let path = `/${company}/users/${uid}/public/avatarUrl`;
return this.af.database.object(path);
}
最后,这是我渲染图像的视图:
<ion-item (click)="openChat(shortChat.id)">
<ion-avatar item-left>
<img [src]="(recipientAvatarUrl$ | async)?.$value" />
</ion-avatar>
</ion-item>
我不明白为什么要用$value来获取节点的值。我认为异步管道会根据 AngularFire2 文档解包 FirebaseObjectObservable。
有线索吗?
如果您要订阅的 FirebaseObjectObservable 节点包含对象,则可以像在 JavaScript 对象中一样访问字段。
<img [src]="(recipientAvatarUrl$ | async)?.name" />
原始值(数字、字符串或布尔值)存储在 $value 下。
<img [src]="(recipientAvatarUrl$ | async)?.$value" />