订阅AngularFireObject

时间:2017-11-23 10:51:05

标签: angular ionic-framework observable angularfire2 angularfire5

我刚刚升级到AngularFire2 rc 5.0。

我调整了我的代码,使其与新的类型和函数相匹配,但在尝试订阅.object(path)时出现以下错误:

Property 'subscribe' does not exist on type 'AngularFireObject'

我的代码如下。

提供者:

getEvent(id: string): AngularFireObject<any> {
    let path = `/events/${id}`;
    return this.af.object(path).valueChanges();
  }

网页:

...
event$: AngularFireObject<any>;
...

// Retrieve event's info
this.event$ = this.eventService.getEvent(this.id);

// Retrieve event's ownership info
let subscription = this.event$.subscribe(event => {
  this.owner$ = this.userService.getUserPublicInfo(event.owner);
});

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您正在使用valueChanges(),意味着getEvent(id: string)正在返回FirebaseObjectObservable<any>而不是AngularFireObject<any>。 检查here

AngularFireObject<any>

返回

this.af.object(path)

更改功能的返回类型。

getEvent(id: string): FirebaseObjectObservable<any> {
    let path = `/events/${id}`;
    return this.af.object(path).valueChanges();
  }