简体   繁体   English

AngularFire2检索不订阅更改(Firestore)

[英]AngularFire2 retrieve don't subscribe to changes (Firestore)

In my Website, I want to display a List of Items by their Name. 在我的网站中,我想按名称显示项目列表。 The names are clickable and then the entire doc should be downloaded and you can edit and save the item. 名称是可点击的,然后应下载整个文档,您可以编辑和保存该项目。

Currently I'm querying collection Data from my FireStore with the way described in the docs: https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md 目前我正在使用文档中描述的方式从FireStore查询集合数据: https//github.com/angular/angularfire2/blob/master/docs/firestore/collections.md

  private itemsCollection: AngularFirestoreCollection<Item>;
  items: Observable<Item[]>;
  constructor(private afs: AngularFirestore) {
    this.itemsCollection = afs.collection<Item>('items');
    this.items = this.itemsCollection.valueChanges();
  }

The problem is, I do not want or need realtime Updates for my list. 问题是,我想要或不需要我的列表的实时更新。 So the Observable approach doesn't really make sense for my app. 所以Observable方法对我的应用程序没有任何意义。

The only Way I came up with is that I just use the snapshotChanges Method, and map it to an array, which I then assign to a field. 我想出的唯一方法是我只使用snapshotChanges方法,并将其映射到一个数组,然后我将其分配给一个字段。 Afterwards the mapping function, sets the items and itemsCollection fields to null, so they don't waste any more bandwidth. 之后映射函数将items和itemsCollection字段设置为null,这样它们就不会浪费更多的带宽。 But that doesn't seem right to me. 但这对我来说似乎不对。

Is there a way to just Query a Collection once, preferrably with Query Parameters, in AngularFire and then display it? 有没有办法在AngularFire中一次查询一个集合,最好是查询参数,然后显示它?

One way I have handled this is by using the rxjs/add/operator/first . 我处理这个问题的一种方法是使用rxjs/add/operator/first This method fetches the Observable data once then ends the subscription. 此方法获取Observable数据,然后结束订阅。

just import that library above and make your calls like so: 只需导入上面的库并像这样拨打电话:

this.items = this.itemsCollection.valueChanges().first();

then you could subscribe to that and it will only fetch the data once: 然后你可以订阅它,它只会获取一次数据:

this.items.subscribe((items) => { // do something with items array // });

I know there are other methods to handle this using the rxjs do() and take() methods as you may want to look into those. 我知道还有其他方法可以使用rxjs do()take()方法来处理这个问题,因为您可能需要查看这些方法。 I hope this can be of help! 我希望这可以有所帮助!

As Nicholas Pesa already answered, using the .first() operator could work, the only thing to note is that it doesn't unsubscribe after emitting a value. 正如Nicholas Pesa已经回答的那样,使用.first()运算符可以正常工作,唯一需要注意的是它在发出值后不会取消订阅。 If you use the .take(integer) operator though as .take(1), the observable will complete after emitting the first value. 如果使用.take(整数)运算符作为.take(1),则observable将在发出第一个值后完成。 That was incorrect. 那是不对的。 The below note about observables was and is valid though. 以下关于可观察量的注释是有效的。

By going this way, you are actually making things more difficult for yourself at almost no benefit. 通过这种方式,你实际上让事情变得更加困难,几乎没有任何好处。 The observable approach makes things easier, even if you won't really be utilizing the observable stream approach. 即使您不会真正使用可观察流方法,可观察的方法也会使事情变得更容易。 Just let the async pipe ( | ) take care of your subscriptions for you, that way you don't have to worry about subscribing or unsubscribing. 只需让异步管道(|)为您处理订阅,这样您就不必担心订阅或取消订阅。 And if your data isn't changing, then the template will only call upon that collection in firestore the first time. 如果您的数据没有变化,那么模板将仅在第一次在firestore中调用该集合。

If you want, use the standard Firebase SDK for the Web, which offers promiss-based API (check the docs here ) so you can create a getter of type Promise in your service (Or Observable.fromPromise() that will emmit only once): 如果需要,可以使用Web的标准Firebase SDK,它提供基于promiss的API(请查看此处的文档),这样您就可以在服务中创建Promise类型的getter(Or Observable.fromPromise()只会发出一次) :

import * as firebase from 'firebase';

const itemsRef = firebase.firestore().collection('items');

// Into your service:
getItems(): Promise<Item[]> {
  const items: Item[];
  return itemsRef.get()
  .then(colSnap => colSnap.docs().map(snapshot => snapshot.data()));
}

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

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