简体   繁体   English

是否可以使用rxjs take运算符来限制从服务返回的可观察值?

[英]Is it possible to use rxjs take operator to limit observables returned from a service?

I am very new to angular and i'm learning by working on website that requires me to list a limited list of 4 cars on the home page, so i created a service that fetches all the cars as shown below. 我对angular非常陌生,我正在网站上学习,该网站要求我在首页上列出4辆车的有限列表,因此我创建了一项提取所有车的服务,如下所示。

import { Response } from '@angular/http';
import { Car } from '../classes/car';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';

@Injectable()
export class CarService {

rootURL = 'http://localhost:3000/cars';

constructor(private _http: HttpClient) {}

getCars(): Observable<Car[]> {
 return this._http.get<Car[]>(this.rootURL);
}
}

and then on my featured cars component, i access the service on component initialization,like shown below 然后在我的特色汽车组件上,我在组件初始化时访问该服务,如下所示

constructor(private _carService: CarService ) { }

ngOnInit() {
  this._carService.getCars()
  .take(4)
  .subscribe(data => this.cars = data,
  error => this.errorMessage = <any>error);
}

So the featured cars components works but it gives me a list of all the cars from the API while i only need to show 4 cars, i have tried using Rxjs 'take` operator and doesn't seem to work? 因此,精选的汽车组件可以工作,但是它为我提供了API中所有汽车的列表,而我只需要显示4辆汽车,我尝试使用Rxjs'take`运算符,但似乎不起作用吗? where might be going wrong? 哪里可能出问题了?

You can use JavaScript's slice operator. 您可以使用JavaScript的slice运算符。

ngOnInit() {
  this._carService.getCars()
  .take(4)
  .subscribe(
     data => this.cars = data.slice(0, 4),
     error => this.errorMessage = <any>error);
}

Emit provided number of values before completing. 完成之前,请发出提供的值数。

Why use take 为什么要用

When you are interested in only the first set number of emission, you want to use take. 当您仅对第一个设置的发射数量感兴趣时,您想使用take。 Maybe you want to see what the user first clicked on when he/she first entered the page, you would want to subscribe to the click event and just take the first emission. 也许您想查看用户首次进入页面时第一次点击的内容,您可能希望订阅click事件并只进行第一次发射。 There is a race and you want to observe the race, but you're only interested in the first who crosses the finish line. 有一场比赛,您想观看比赛,但是您只对第一个越过终点线感兴趣。 This operator is clear and straight forward, you just want to see the first n numbers of emission to do whatever it is you need. 该运算符明确而直接,您只想查看前n个发射数即可完成所需的任何操作。

taken from learn-rxjs 取自learn-rxjs

the 'take' operator isn't used to limit the number of data from the returned array. 'take'运算符不用于限制返回数组中的数据数量。 You could use a normal JavaScript function Array.slice to manipulate the array. 您可以使用普通的JavaScript函数Array.slice来操纵数组。

ngOnInit() {
    this._carService.getCars()
    .subscribe(data => this.cars = data.slice(0, 4),
    error => this.errorMessage = <any>error);
}

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

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