【发布时间】:2018-05-09 02:28:19
【问题描述】:
从我的组件开始,我声明了 observable...
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import { tap, map, concat } from 'rxjs/operators';
contactsDataSource$: Observable<any>;
然后我将流分配给来自 http 服务的 observable:
this.contactsDataSource$ = this.medialistsrv.getMediaListBuilderData({
mediaListId: this.mediaListId,
sortType: this.sortType,
startRow: this.startRow,
numRows: this.numRows
}).pipe(
tap(response => {
this.mediaList = response
.filter(items => items.inMediaList === true)
.map(items => items.contactId);
this.mediaList.forEach(id => {
this._contacts.push(this.fb.control(id));
});
}),
map(response => response)
);
并使用异步管道将其引入模板:
<div *ngIf="(contactsDataSource$ | async) as contactList; else loading">
稍后在模板中我介绍了一种加载更多结果的方法。这是我需要连接到初始流的地方
increment() {
this.contactsDataSource$.concat(
this.medialistsrv.getMediaListBuilderData({
mediaListId: this.mediaListId,
sortType: this.sortType,
startRow: (this.numRows + 1),
numRows: (this.numRows * 2)
}).map(response => response)
);
}
运行此程序时,我在控制台中收到一条错误消息:
ng:///MediaListModule/MediaListBuilderComponent.ngfactory.js:351 ERROR TypeError: this.contactsDataSource$.concat is not a function
我还在 rxjs 学习悬崖上,所以显然我不明白如何正确地做到这一点。我在这里错过了什么?
【问题讨论】:
-
您正在混合使用 pipable 和“原型”运算符。使用
this.contactsDataSource$.pipe(concat(...), map(...)) -
这消除了错误,但实际上并没有在显示中连接新流。