繁体   English   中英

Angular - 找出派生的 class 是否实现了抽象 class (router.component)

[英]Angular - find out if derived class implements an abstract class (router.component)

我正在尝试使用router.component来设置页面标题。 所以,我创建了一个抽象的 class ,它必须提供标题:

export abstract class ComponentWithTitleBase {
  abstract get title(): Observable<string>;
}

然后实现它的组件如下所示:

export class AboutComponent extends ComponentWithTitleBase implements OnInit {
  get title(): Observable<string> {
    return of("About the demo");
  }

  ngOnInit(): void {}
}

现在我只需要将它与路由器事件一起使用。 我订阅并查看router.component是否为ComponentWithTitleBase ,所以:

    this.router.events
      .pipe(
        // identify navigation end
        filter((event) => event instanceof NavigationEnd),
        // now query the activated route
        map(() => this.rootRoute(this.route)),
        filter((route) => !!route.component),
        tap((route: ActivatedRoute) =>
          console.log(route.component instanceof ComponentWithTitleBase)
        ),
...

但无论我做什么 output 都是false 有什么办法可以找出某些 class 是否实现了另一个抽象class?

instanceof不适用于抽象 class,

route.component instanceof ComponentWithTitleBase替换为ComponentWithTitleBase.isPrototypeOf(route.component)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM