【发布时间】:2020-07-27 19:07:52
【问题描述】:
我正在尝试使用router.component 设置页面标题。所以,我创建了一个必须提供标题的抽象类:
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)
),
...
但无论我做什么输出都是false。有什么方法可以找出某个类是否实现了另一个 abstract 类?
【问题讨论】:
标签: angular typescript abstract-class