无法对 Angular 4 数字求和

弗朗西斯科·加西亚·孔特雷拉斯

我正在学习 Angular4 并且我正在尝试做一个简单的总和,但是在进行计算时,它会连接数字并且不会将它们相加。有谁知道进行这项练习的正确方法是什么?

<h2 class="nombre">suma: {{ suma() }}</h2>

export class AppComponent {
  title = 'app';
  nombre: string;
  numero1: number;
  numero2: number;
  numero3: number;
  constructor() {
    this.numero1 = 0;
    this.numero2 = 0;
  }
  suma (): number {
    this.numero3 = (this.numero1 + this.numero2);
    return  this.numero3;
  }
}
胡安德

尝试将数字类型从stringnumber

<h2 class="nombre">suma: {{ suma() }}</h2>

export class AppComponent {
  title = 'app';
  nombre: string;
  numero1: number;
  numero2: number;
  numero3: number;
  constructor() {
    this.numero1 = 0;
    this.numero2 = 0;
  }
  suma (): number {
    this.numero3 = (Number(this.numero1) + Number(this.numero2));
    return  this.numero3;
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章