vue3+ts は parseInt を使用します。タイプ 'number' の引数は、タイプ 'string' のパラメータに割り当てることができません。

シーン

在之前习惯使用javascript开发的时候,直接使用parseInt将数字转为整数。而在使用typescript开发时,却出现了报错。
报错内容:Argument of type 'number' is not assignable to parameter of type 'string'.

エラーの理由

parseInt(string, radix) 函数解析字符串并返回整数。第一个参数为要解析的字符串,第二个参数为要转换的进制基数,默认为十进制。
javascript里会自动对参数进行隐式转换,因此使用parseInt(100)并不会报错,而typescript时报错了。

解決

1. toString は文字列に変換されます

const data = parseInt((Math.random() * num).toString());

2. Math.floor() メソッドを使用する

const data = parseInt(Math.floor((Math.random() * num)));

おすすめ

転載: blog.csdn.net/zhongxiajiu/article/details/130624283