Upgrading to AngularFire 5.0

2019-03-06 10:50发布

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条回答
我只想做你的唯一
2楼-- · 2019-03-06 11:10

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);
  })
查看更多
登录 后发表回答