mergeMap(names => names),
的语法有什么问题?
getIds(): void {
this.myservice
.getIds.pipe( mergeMap(id => this.getNames(id)),
mergeMap(names => names), toArray() )
}
myservce.ts具有以下内容:
getIds():Observable<Ids>{
const url = 'http://localhost:4000';
return this.http
.post(url, '')
.map(({ Ids }: any) => Ids.map(item => ({Id: item.Id,
Name: item.Name }))) as Observable<Ids>;
}
getNames(data: Ids):Observable<any[]> {
const url ='http://localhost:5000';
return this.http
.post(url,data)
.pipe(map(({ Results }: any) => Results[0].results.map(item => ({Id: item.ID, Name: item.Name }))));
}
编译器错误是(作为新手,我无法弄清楚):
ERROR in src/app/cvetable/mcomponent.ts(132,19): error TS2345:
Argument of type '(names: {}) => {}' is not assignable to parameter
of type '(value: {}, index: number) => ObservableInput<any[]>'.
Type '{}' is not assignable to type 'ObservableInput<any[]>'.
Type '{}' is not assignable to type 'Iterable<any[]>'.
Property '[Symbol.iterator]' is missing in type '{}'.
答案 0 :(得分:0)
您的服务中的getIds
方法存在严重问题:
应该是这样。由于您使用的是HttpClient
,并且您的示例位于使用Rxjs 6+的Angular 7中,因此必须将.pipe
调用链接到您的Observable值,以便对其应用运算符。在.pipe
调用中,您可以放置一个逗号分隔的运算符列表:
getIds(obj):Observable<Ids>{
return this.http
.post(this.url, obj)
.pipe(
map((Ids: any) => Ids.map(item => ({Id: item.Id, Name: item.Name }) ) )
)
}
这是一个Updated StackBlitz,已修复错误。
答案 1 :(得分:0)
您应该使用switchMap
而不是mergeMap
getIds(): void {
this.myservice
.getIds.pipe(switchMap(id => this.getNames(id)))
}