angular8 es5子调用父方法调用this.method是未定义的

问题描述 投票:0回答:2
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错误,TypeError:this.getFirstName不是函数

为什么?

angular angular8 ecmascript-5 es5-shim
2个回答
0
投票

起初,我建议您在返回数据时为方法提供返回类型。

export class ParentComponent {
  public getName(): string {
      return this.getFirstName() + this.getLastName(); // <--- exception, core.js:4002 ERROR TypeError: this.getFirstName is not a function
  }

  public getFirstName(): string {
      return 'wu';
  }

  public getLastName(): string {
      return 'victor';
  }
 }

0
投票

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

儿童构造函数应调用ParentComponent,找到引用的代码

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

希望这可以解决您的问题...并且在ts / js天气中,提及返回类型与否无关紧要,除非您严格要求特定的响应类型

© www.soinside.com 2019 - 2024. All rights reserved.