angular viewportScroller捕获以不兼容类型结束的事件

angular viewportScroller捕获以不兼容类型结束的事件,angular,Angular,我尝试使用Angular docs中的示例来实现向后导航时的滚动: class AppModule { constructor(router: Router, viewportScroller: ViewportScroller) { router.events.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)).subscribe(e => { if (e.position) {

我尝试使用Angular docs中的示例来实现向后导航时的滚动:

class AppModule {
  constructor(router: Router, viewportScroller: ViewportScroller) {
    router.events.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)).subscribe(e => {
      if (e.position) {
        // backward navigation
        viewportScroller.scrollToPosition(e.position);
      } else if (e.anchor) {
        // anchor navigation
        viewportScroller.scrollToAnchor(e.anchor);
      } else {
        // forward navigation
        viewportScroller.scrollToPosition([0, 0]);
      }
    });
  }
但在编译时,我最终会出现以下错误:

 ERROR in src/app/app.module.ts(23,24): error TS2345: Argument of type 'MonoTypeOperatorFunction<Event>' is not assignable to parameter of type 'OperatorFunction<Event, Event>'.
      Types of parameters 'source' and 'source' are incompatible.
        Type 'import("/Users/PROJECT_PATH/node_modules/rxjs/internal/Observable").Observable<import("/Users/PROJECT_PATH/node_modules/@angular/router/router").Event>' is not assignable to type 'import("/Users/PROJECT_PATH/node_modules/rxjs/internal/Observable").Observable<Event>'.
          Type 'import("/Users/PROJECT_PATH/node_modules/@angular/router/router").Event' is not assignable to type 'Event'.
            Type 'ActivationEnd' is missing the following properties from type 'Event': bubbles, cancelBubble, cancelable, composed, and 18 more.
    src/app/app.module.ts(23,48): error TS2677: A type predicate's type must be assignable to its parameter's type.
      Type 'Scroll' is missing the following properties from type 'Event': bubbles, cancelBubble, cancelable, composed, and 18 more.
    src/main.ts(4,10): error TS2305: Module '"./app/app.module"' has no exported member 'AppModule'.
src/app/app.module.ts(23,24)中的错误:错误TS2345:类型为“MonoTypeOperatorFunction”的参数不能分配给类型为“OperatorFunction”的参数。
参数“源”和“源”的类型不兼容。
类型“导入(“/Users/PROJECT\u PATH/node\u modules/rxjs/internal/Observable”).Observable”不可分配给类型“导入(“/Users/PROJECT\u PATH/node\u modules/rxjs/internal/Observable”).Observable”。
类型“导入”(/Users/PROJECT\u PATH/node\u modules/@angular/router/router”)。事件不可分配给类型“Event”。
类型“ActivationEnd”缺少类型“Event”中的以下属性:气泡、cancelBubble、cancelable、Composited等。
src/app/app.module.ts(23,48):错误TS2677:类型谓词的类型必须可分配给其参数的类型。
类型“Scroll”缺少类型“Event”中的以下属性:气泡、cancelBubble、cancelable、Composited等。
src/main.ts(4,10):错误TS2305:模块“”。/app/app.Module“”没有导出的成员“AppModule”。
将(e:Event)更改为仅(e)起作用

类型为“MonoTypeOperatorFunction”的参数不能分配给类型为“OperatorFunction”的参数。

我猜有两种不同类型的事件,并且筛选器接收的事件与router.events中包含的事件类型不同


不声明接收的类型可以解决此问题

您也可以尝试以下方法:(e:RouterEvent | Scroll):e is Scroll

您是否可以将您的筛选器部分更改为以下格式并查看其是否有效:将:router.events.pipe(筛选器((e:Event):e is Scroll=>e instanceof Scroll))替换为:router.events.pipe(筛选器((e:e is Scroll=>e instanceof Scroll))是的,请考虑添加解释,并用“更好地格式化你的答案”。