Upgrading to AngularFire 5.0

吃可爱长大的小学妹 提交于 2019-12-02 16:13:37

问题


I'm using ionic 3 with firebase.

Until now I use angularfire 4.0 and the following code gave me an observable for the data from firebase:

  obsToData: FirebaseObjectObservable<any>;

  constructor(public nav: NavController, public shared: SharedProvider, 
              public DB: AngularFireDatabase) {

              this.obsToData = DB.object('/myData');
  }

now, according to this page FirebaseObjectObservable removed and I need to use AngularFireObject instead, how can I get the data?

I did the following change:

obsToData: AngularFireObject<any>;

  constructor(public nav: NavController, public shared: SharedProvider,
              public DB: AngularFireDatabase) {

              this.obsToData = DB.object('/myData');
  }

but I don't find the way how to get an observable from this new object to my data from firebase.

Does someone succeed to use angularfire 5.0?


回答1:


you need to use valueChanges() to get the Observable from the AngularFireDatabase Object reference.

obsRef: AngularFireObject<any>;
obsToData: Observable<any>;

  constructor(public nav: NavController, public shared: SharedProvider,
              public DB: AngularFireDatabase) {

              this.obsRef = DB.object('/myData');//reference
              this.obsToData = this.obsRef.valueChanges();//Observable
  }

EDIT to get data and save it,subscribe like any observable

  this.obsToData.subscribe(data=>{
       console.log(data);
  },error=>{
       console.log(error);
  })


来源:https://stackoverflow.com/questions/47449756/upgrading-to-angularfire-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!