【发布时间】:2018-09-19 14:37:28
【问题描述】:
我正在寻找一种比现有解决方案更具可读性的解决方案。
我需要: 1) 从 API 中检索产品。它们是一组对象。 2)根据类别等过滤这些产品...... 3) 对产品进行分页并返回这些产品的分页版本。
ngOnInit() {
//This gets the products from the API
this.drinkSubscription = this.drinkService.getAllDrinks().subscribe(drinks => {
//Save products without pagination for other purposes
this.allDrinks = drinks;
//Get the parameter to filter products
this.paramSubscription = this.route.params.subscribe((params: Params) => {
//Filter the products and return a filtered array
const filteredDrinks = this.filterService.filter(drinks, params['filter'], params['name']);
//Sort products based on the selection
this.sorterSubscription = this.sorter.initialize(filteredDrinks).subscribe(sortedDrinks => {
//Create a pager that holds the paginated drinks in a property
this.pagerSubscription = this.pagerService.initializePaginatedItems(sortedDrinks, 10, 5)
.subscribe(pager => {
this.pager = pager;
this.paginatedDrinks = pager.paginatedItems;
});
});
});
});
}
排序器和分页是 BehaviorSubjects 以便我可以注入 next() 但我对它们并不积极... 您可以看到缩进级别相当高,我想知道 RxJS 是否有办法以更易读的方式获得相同的结果。
【问题讨论】:
标签: javascript stream rxjs rxjs6