在Angular(v5.2)中,我有路由器的解析器实现。
@Injectable()
export class MyCustomRouteResolver implements Resolve<Observable<String>> {
resolve(router: ActivatedRouteSnapshot, routerState: RouterStateSnapshot): Observable<String> {
let componentName:string = (<Type<any>>router.component).name; //gives the class name of the component
//some logic
}
}
我的AngularComponent从名为BaseComponent
的类扩展而来,该类具有getId()
函数。
现在,当我尝试下面的代码时
if(router.component instanceof BaseComponent){
//some logic
}
,以上条件不满足。如何获得对BaseComponent
的引用,以便可以调用getId()
函数?像下面这样
let baseComp:BaseComponent = (<BaseComponent>)router.component;
console.log(baseComp.getId());