totalbalancetemp = (Number(this.balance)) + (Number(this.pastAmount));
我的 totalbalancetemp
返回未定义,而 this.balance
等于 34,this.pastAmount
等于 23。
我在 Controller 中有这个,并使用 html 中的 exp 显示 totalbalancetemp
最佳答案
提供正确的类型。
let totalbalancetemp:number = balance + pastAmount
这将引发错误,因为您现在保证 totalbalancetemp
将是一个数字
。
The type String is not assignable to type 'number'
尝试以下操作:
let balance:string = '34',
pastAmount:string = '23',
totalbalancetemp:number = 0
totalbalancetemp = Number(balance) + Number(pastAmount)
alert(totalbalancetemp)
关于javascript - typescript - 将字符串转换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666343/