angular - Angular Rxjs:使用管道运算符的语法错误
问题描述
我正在尝试重新编写此代码:
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)) <<***>>
);
我收到编译器错误<<***>>
:
'响应 | 类型的参数 “页面”不可分配给“页面”类型的参数。“响应”类型缺少“页面”类型的以下属性:总计、用户
似乎catchError
是返回{} | Page
类型,当它应该返回一个Page
类型时。
有任何想法吗?
解决方案
您错过了将 a 映射response
到 a page
。
merge(this.searchQuery$, this.lazyQuery$).pipe(
tap(() => this.loadingPage()),
map(filter => this.buildURL("jaume", Config.security['appName'], filter)),
switchMap(url => this.service.getItemsFromStorage(url).pipe(
map(response => this.buildPage(response)), // <-- you missed this map in your code
catchError(() => of(pojo.Page.EMPTY))
)),
tap(page => this.loadedPage(page)),
takeUntil(this.unsubscribe$)
).subscribe();
另请查看官方迁移指南:Howto: Convert to pipe syntax
推荐阅读
- meteor - Meteor - 发布所有作者的最佳方法,仅发布他们的最后一篇文章
- wildfly - CLI 脚本 - 使用环境属性启用提供程序
- sql - PostgreSQL中的全文搜索,根据每个关键字的不同分数进行自定义排名
- r - 使用 R 中的 if 语句从栅格创建新栅格
- xamarin.android - Messenger:如何等待消息被处理?
- php - 无法从android中的服务器解析日期时间
- php - 此页面包含以下错误:第 2 行第 6 列的错误:XML 声明只允许在文档的开头
- java - 如何将位图保存到android中的应用程序文件夹
- javascript - 如何将数据从父 vuejs 传递给子组件
- r - 使用多个列和值进行更长的旋转