简体   繁体   English

AngularFire2 v5.0对象

[英]AngularFire2 v5.0 objects

So i am trying to wrap my head around AngularFire2. 所以我想把我的头缠在AngularFire2上。 I started with version 5.0 and so many things have changed lately, that most things on the internet are outdated. 我从5.0版开始,最近发生了很多变化,以至于Internet上的大多数东西已经过时了。

All i really want to achieve right now is retrieving an object from my Angular database and pass the data to my template. 我现在真正想要实现的只是从Angular数据库中检索一个对象并将数据传递到我的模板。 Sounds easy, but it apparently is not. 听起来很容易,但显然并非如此。 There is close to no documentation. 几乎没有文档。

I tried to write a getData function like so: 我试图编写一个getData函数,如下所示:

getData(objPath: string): AngularFireObject<any> {
    // console.log(this.db.list(objPath).valueChanges());
    return this.db.object(objPath).valueChanges();
  }

I am getting this error: 我收到此错误:

Type 'Observable<{}>' is not assignable to type 'AngularFireObject'. 类型“ Observable <{}>”不可分配给类型“ AngularFireObject”。 Property 'query' is missing in type 'Observable<{}>'. 类型“ Observable <{}>”中缺少属性“查询”。

I tried swapping out the valueChanges() part with subscribe() or others but it does not work. 我试过将valueChanges()部分换成subscription( subscribe()或其他部分,但它不起作用。 And if i just log return this.db.object(objPath); 如果我只记录return this.db.object(objPath); without anything else i get something like this : {"query":"https://blabla.firebaseio.com/users/Vi37EbcH"} 我什么也没有得到: {"query":"https://blabla.firebaseio.com/users/Vi37EbcH"}

How can i query this specific object @ users/Vi37EbcH ? 如何查询此特定对象@ users / Vi37EbcH? Please help. 请帮忙。

I have made a full example of fetching a list and one item. 我已经举了一个完整的例子来获取一个列表和一个项目。 I used snapshotChanges to be able to add ID to each item, because its very common to click on an item from a list, and then fetch that item from the ID. 我使用snapshotChanges能够向每个项目添加ID,因为单击列表中的项目然后从ID中获取该项目非常常见。 Also, using map() will allow you to convert the item before it reaches the template. 另外,使用map()将允许您在项目到达模板之前对其进行转换。

I also use subscribe to get the result out of the observable, ready to be used in a template. 我还使用订阅从可观察的结果中获取结果,准备在模板中使用它。 Here is the example: 这是示例:

import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
import { Activity } from '../shared/interfaces/Activity';

@Component({
  selector: 'app-activities',
  templateUrl: './activities.component.html',
  styleUrls: ['./activities.component.scss']
})
export class ActivitiesComponent implements OnInit {

  activitiesCollection: AngularFirestoreCollection<Activity>;
  activitiesDoc: AngularFirestoreDocument<Activity>;
  itemList: Activity[];
  editItem: Activity;

  constructor(public db: AngularFirestore) { }

  ngOnInit() {
    this.activitiesCollection = this.db.collection('activities');
  }

  getActivities() {
    this.activitiesCollection
      .snapshotChanges()
      .map(arr => {
        return arr.map(snap => {
          const obj = snap.payload.doc.data() as Activity;
          obj.id = snap.payload.doc.id;
          return obj;
        });
      })
      .subscribe(response => {
        console.log('getActivities4: subscribe: response: ', response);
        this.itemList = response;
      });
  }

  getActivity(id) {
    this.activitiesDoc = this.activitiesCollection.doc(id);
    this.activitiesDoc
      .snapshotChanges()
      .map(snap => {
        if (snap.payload.exists) {
          const obj = snap.payload.data() as Activity;
          obj.id = snap.payload.id;
          return obj;
        }
      })
      .subscribe(response => {
        this.editItem = response;
      });
  }


}

I may have fixed it like so : 我可能已将其固定为:

constructor(private db: AngularFireDatabase, private route: ActivatedRoute) {
    this.route.params.subscribe(params => (this.path = `users/${params.id}`));
    const userobject = db.object<any>(this.path);
    const users$: Observable<any[]> = userobject.valueChanges();
    users$.subscribe(console.log);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM