首页 > 解决方案 > angular8 es5子调用父方法调用this.method未定义

问题描述

export class SonComponent extends ParentComponent {

 public say() {
     console.info('hello!my parent is ' + super.getName());
 }
}
export class ParentComponent {
  public getName() {
    return this.getFirstName() + this.getLastName(); // <--- exception, core.js:4002 ERROR TypeError: this.getFirstName is not a function
  }
  public getFirstName() {
    return 'wu';
  }
  public getLastName() {
    return 'victor';
  }

异常,core.js:4002 错误类型错误:this.getFirstName 不是函数

为什么?

标签: angularangular8ecmascript-5es5-shim

解决方案


你的组件中的构造函数在哪里......???当你扩展 ParentComponent

childs 的构造函数应该调用 ParentComponent,找到 ref 的代码

export class SonComponent extends ParentComponent {
 constructor(){
  super();
  }    
 public say() {
     console.info('hello!my parent is ' + super.getName());
 }
}

希望这可以解决您的问题...并且在 ts/js 天气中,您是否提到返回类型并不重要,除非您严格期望特定类型的响应


推荐阅读