【发布时间】:2017-09-15 03:56:21
【问题描述】:
我正在尝试过滤角度事件,但 typescript 不喜欢它。使用此代码:
this.router.events
.filter(e => e instanceof RoutesRecognized)
.subscribe(data => {
console.log(data.state.root.firstChild.data[0])
});
工作并输出我放入路由配置的任何静态数据,例如:
{ path: 'about', component: AboutContainer, data: [{ Foo: 'bar' }] }
除了 typescript 会抛出错误信息:
error TS2339: Property 'state' does not exist on type 'Event'.
Property 'state' does not exist on type 'NavigationStart'.
所以它工作正常,但错误消息很烦人。我可以将 console.log 包装在 if data instanceof RoutesRecognized 中,但看起来很脏。有谁知道如何使用 RxJS 停止错误?
【问题讨论】:
-
可以将类型改为
any,.subscribe(data: any => { -
当我将它转换为任何它会引发错误:
ReferenceError: data is not defined和error TS1005: ',' expected. -
:e => e应该:必须在那里我怀疑它。 -
@JohnPaulVaughan
.subscribe((data: any) => {怎么样? -
感谢@echonax - 包装它是一种享受。
.subscribe((data: any) => {
标签: angular filter event-handling rxjs