rxjs 和 angular 2 以及 "take" 运算符的使用

rxjs and angular 2 and the use of the "take" operator

我正在尝试在我的代码中使用 "take" 运算符(学习 rxjs),但它并没有像我想要的那样发送前 5 个。我的简单代码如下,有人知道如何提供帮助吗?

countries: Observable<Country[]>;

private searchTerms = new Subject<string>();

this.countries = this.searchTerms.debounceTime(300).distinctUntilChanged().switchMap(  
            searchTerm => searchTerm ? this.countrySearchService.search(searchTerm) : observable.of<Country[]>([])) 
            .take(5);

阅读您的评论后,我了解到您需要前 5 个国家/地区。现在请注意,您的可观察对象发出国家数组而不是国家数组。您使用 Observable.of 而不是 Observable.from 的原因。所以,正确的语法应该是:

this.countries = this.searchTerms.debounceTime(300).distinctUntilChanged().switchMap(  
        searchTerm => searchTerm ? this.countrySearchService.search(searchTerm) : observable.from<Country[]>([])) 
        .take(5);

如果您想要演示各种用例的示例,请查看 this jsbin