Angular Rxjs:使用可管道运算符时出现语法错误
我正在尝试重新编写此代码:Angular Rxjs:使用可管道运算符时出现语法错误,angular,typescript,rxjs,Angular,Typescript,Rxjs,我正在尝试重新编写此代码: Observable .merge(this.searchQuery$, this.lazyQuery$) .do(() => this.loadingPage()) .map(filter => this.buildURL("jaume", Config.security['appName'], filter)) .switchMap(url => this.service.getItemsFromS
Observable
.merge(this.searchQuery$, this.lazyQuery$)
.do(() => this.loadingPage())
.map(filter => this.buildURL("jaume", Config.security['appName'], filter))
.switchMap(url =>
this.service.getItemsFromStorage(url)
.map(response => this.buildPage(response))
.catch(() => Observable.of(pojo.Page.EMPTY))
)
.do(page => this.loadedPage(page))
.takeUntil(this.unsubscribe$)
.subscribe();
我想使用“pipable”语法。到目前为止,我已经能够写下:
this.searchQuery$.pipe(
merge(this.lazyQuery$),
tap(() => this.loadingPage()),
map(filter => this.buildURL("jaume", Config.security['appName'], filter))
)
.pipe(
switchMap(url => this.service.getItemsFromStorage(url)),
catchError(() => Observable.of(pojo.Page.EMPTY))
)
.pipe(
tap(page => this.loadedPage(page)) <<***>>
);
this.searchQuery$.pipe(
合并(this.lazyQuery$),
轻触(()=>this.loadingPage()),
映射(filter=>this.buildURL(“jaume”,Config.security['appName'],filter))
)
.烟斗(
switchMap(url=>this.service.getItemsFromStorage(url)),
catchError(()=>可观察的(pojo.Page.EMPTY))
)
.烟斗(
轻触(第=>this.loadedPage(第页))
);
我在
上收到一个编译器错误:
“Response | Page”类型的参数不能分配给“Page”类型的参数。
类型“Response”缺少类型“Page”中的以下属性:总计,用户
似乎catchError
正在返回{}页面
类型,而它应该返回单个页面
类型
有什么想法吗?首先,管道太多了。没有必要这么多。您也可以使用
of()
而不是Observable.of()
,后者非常简短且易于阅读
this.searchQuery$.pipe(
merge(this.lazyQuery$),
tap(() => this.loadingPage()),
map(filter => this.buildURL("jaume", Config.security['appName'], filter)),
switchMap(url => this.service.getItemsFromStorage(url)),
catchError(() => of(pojo.Page.EMPTY)),
tap(page => this.loadedPage(page)) <<***>>
);
当你这样做的时候
.pipe(...),
.pipe(...),
.pipe(...),
这相当于
.pipe(
...
...
...
)
您没有将
响应
映射到页面
merge(this.searchQuery$,this.lazyQuery$).pipe(
轻触(()=>this.loadingPage()),
映射(filter=>this.buildURL(“jaume”,Config.security['appName'],filter)),
switchMap(url=>this.service.getItemsFromStorage(url).pipe(
map(response=>this.buildPage(response)),//of(pojo.Page.EMPTY))
)),
点击(页面=>this.loadedPage(页面)),
takeUntil(此.取消订阅$)
).subscribe();
还可以查看官方的迁移指南:您不需要那么多管道。一个管道只够一个工作流程。你能提供一个说明你方法的答案吗?我错过了很多关于你方法的细节。1) 那么嵌套的`switchMap(…).catch(…)方法呢?保留此嵌套链非常重要,否则当出现错误时,订阅将被删除。2) 我认为TypeScript不会思考,也不会。以防万一,我已经测试了你的方法,但问题仍然存在。抱歉。此错误是一个类型脚本erorr。这个错误也清楚地指出了问题的位置。如果您使用一些功能强大的IDE,比如WebStorm,它还应该通过代码分析显示它的位置
.pipe(...),
.pipe(...),
.pipe(...),
.pipe(
...
...
...
)