![](/img/trans.png)
[英]Karma unit testing - finding out which abstract class a component implements
[英]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.