在Angular中,如何从类中调用虚方法而不是在父类中继承?

时间:2017-05-18 16:49:37

标签: angular typescript inheritance extends virtual-method

我有两个继承一个基类的组件:

  1. ChildA 扩展基础
  2. 班级 ChildB 扩展基础
  3. 为了对某些方法进行分解,我想从 Base 中调用OnChanges()中Childs的foo() 像这样:

    export abstract class Base implements OnChanges{
        ngOnChanges(changes: SimpleChanges) {
            this.foo();
        }
        foo(): void{
           throw("Error, it shouldn't be instantiated. Override this method.");
        }
    }
    
    export class ChildA extends Base{
        ...
        foo(): void{
             alert("This is the correct method which has to be called !");
        }
    }
    

    结果是:

      

    错误,它不应该被实例化。覆盖此方法。

    不是:

      

    这是必须调用的正确方法!

    有人有想法吗?

0 个答案:

没有答案