问题可能出现在Firestore数据库的查询,确保正确使用startAfter(...)方法,并在查询中包含正确的排序字段。下面是一个示例代码:
import { Component, OnInit } from '@angular/core'; import { AngularFirestore } from '@angular/fire/firestore'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators';
@Component({ selector: 'app-products', templateUrl: './products.component.html', styleUrls: ['./products.component.css'] }) export class ProductsComponent implements OnInit { private productsCollection: AngularFirestoreCollection; products$: Observable;
constructor(private afs: AngularFirestore) {}
ngOnInit() { this.productsCollection = this.afs.collection("products", ref => { return ref.orderBy("name"); // 排序字段 }); this.loadProducts(); }
loadProducts() { this.products$ = this.productsCollection .limit(10) .startAfter("lastProduct") // 上一页的最后一条数据 .snapshotChanges() .pipe( map(changes => { return changes.map(a => { const data = a.payload.doc.data(); const id = a.payload.doc.id; return { id, ...data }; }); }) ); } }
在上述代码中,您可以看到使用startAfter方法,该方法接受上一页的最后一项作为参数,并在查询中包含正确的排序字段。此代码应该与Angular Firebase中的分页查询正常工作。