简体   繁体   English

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

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

I'm trying to use router.component to set page title.我正在尝试使用router.component来设置页面标题。 So, I created an abstract class which has to provide title:所以,我创建了一个抽象的 class ,它必须提供标题:

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

and then the component that implements it looks like this:然后实现它的组件如下所示:

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

  ngOnInit(): void {}
}

Now I just need to use it with router events.现在我只需要将它与路由器事件一起使用。 I subscribe and see if router.component is ComponentWithTitleBase , so:我订阅并查看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)
        ),
...

But whatever I do the output is false .但无论我做什么 output 都是false Is there any way I could find out if some class implements another abstract class?有什么办法可以找出某些 class 是否实现了另一个抽象class?

instanceof does not work for an abstract class, instanceof不适用于抽象 class,

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

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

相关问题 Karma 单元测试 - 找出组件实现的抽象类 - Karma unit testing - finding out which abstract class a component implements 我不明白为什么 Angular 组件 class 添加实现 onInit 接口 - I can't figure out why Angular component class add implements onInit interface 测试 Angular 组件扩展抽象 class - Testing Angular component extending an abstract class 在 Angular 6 中的 class 上实现 - Implements on a class in Angular 6 Angular:如何将参数从派生组件类传递给基类 - Angular: How to pass arguments from derived component class to base class 不能在模块声明中使用抽象 class Angular 组件 - Cannot use an abstract class Angular component in a module declaration 如何在抽象类和 Angular 8 中抽象类的实现中使用@Component 装饰器? - How to use @Component decorator in both abstract class and in the implementation of abstract class in Angular 8? 从抽象 Class 与抽象组件继承 angular 组件有什么区别? - What's the difference between inheriting an angular component from an Abstract Class versus an Abstract Component? Angular抽象类HttpClient注入 - Angular abstract class HttpClient injection 如何在 Angular 中使用抽象方法实现抽象 class - How to implement abstract class with abstract method in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM