首页 > 解决方案 > Ionic 3 - 打字稿错误:算术操作数必须是“任何”、“数字”或枚举类型

问题描述

当用户单击类似按钮以增加数字时,我有一个单击操作,但是在构建或运行 ionic 应用程序时,我遇到了打字稿错误,有人可以帮我解决这个问题吗?

Typescript Error
An arithmetic operand must be of type 'any', 'number' or an enum type.
src/pages/comments/comments.ts
document.getElementById("like-" + commentid).innerHTML++

在comments.ts

likedislike(type,commentid) {
  if(type === 'like') {
    document.getElementById("like-" + commentid).innerHTML++
  }
}

标签: typescriptionic-frameworkionic3

解决方案


尝试使用Number()将内部 html 字符串转换为数字。

var x = document.getElementById("like-" + commentid).innerHTML; 

document.getElementById("like-" + commentid).innerHTML = Number(x) + 1

虽然在 Ionic 中使用 Angular 数据绑定来做到这一点要好得多......


推荐阅读