Angular 4 扩展和实施

Angular 4 extend and implement

我有一个 class Hero 需要扩展 GoogleCharts class。我还需要实现 OnInit 以从参数中获取一些数据。

我该怎么做?

就像这样:

export class Hero extends GoogleCharts implements OnInit {...

如果 GoogleCharts 已经实现了 OnInit,您应该先调用 super.ngOnInit();,然后再在 ngOnInit 方法中执行其他操作。

像这样:

interface OnInit {
    ngOnInit: () => void;
}

class GoogleCharts implements OnInit{

    ngOnInit() {
        //does some stuff here
    }

}

class Hero extends GoogleCharts implements OnInit{

    ngOnInit() {
        super.ngOnInit();
        //do my stuff here
    }

}