[英]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.