【发布时间】:2019-06-26 20:00:33
【问题描述】:
我有网络应用程序,使用 Angular 6 和 google-clod-firestore 开发,它接受客户的订单来交付书籍;目前它有两个组件
- CRM 组件
- 下单组件
当客户拨打电话时,会在CRM中弹出,并跳转到下单页面。下单成功后会再次跳转到CRM页面。
下单组件将显示所有可用的书籍,这些书籍来自名为“books”的集合,大小为 1000;
所以每次进入下单页面都会读取1000个文档,这会产生很大的成本。我的问题是如何减少这个读取。是否可以将读取操作限制为一次。?
目前我用shareReplay和singleton服务实现了一个逻辑,不知道是不是正确的实现;
这里是代码
SingeltonService.ts
-------------------------
export class SingeltonService {
source: Observable<any>;
aa: any;
bb: any;
constructor(private afs: AngularFirestore) {
console.log('new instance created!');
this.source = this.afs.collection('test-
collection').valueChanges().pipe(
tap((docs) => { console.log(`Read ${docs.length} docs`); }),
shareReplay(1));
this.aa = this.source.subscribe()
this.bb = this.source.subscribe()
}
}
Order-Plcaing-component.ts
-------------------------
export class OrderPlacingCompnent implements OnInit {
constructor(private singelton: SingeltonService){}
ngOnInit() {}
}
【问题讨论】:
标签: firebase google-cloud-firestore angular6 observable