【问题标题】:why does typescript ignore the RxJS filter为什么打字稿会忽略 RxJS 过滤器
【发布时间】: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 definederror TS1005: ',' expected.
  • :e => e 应该: 必须在那里我怀疑它。
  • @JohnPaulVaughan .subscribe((data: any) => { 怎么样?
  • 感谢@echonax - 包装它是一种享受。 .subscribe((data: any) => {

标签: angular filter event-handling rxjs


【解决方案1】:

@echonax 建议 .subscribe((data: any) => { 效果很好。我最终选择了更具体的 RoutesRecognized。这删除了错误消息:

.subscribe( (data: RoutesRecognized) => {

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 2011-12-15
    • 1970-01-01
    • 2020-10-12
    • 2016-11-02
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多