我正在学习 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;
}
}
尝试将数字类型从string
到number
<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] 删除。
我来说两句