角获取组件的父类引用

时间:2018-09-27 10:51:16

标签: angular

在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());

0 个答案:

没有答案