实现限制price为Number类型 addItemFeWater(key) { let item = { key: key + 1, abbreviation: '', fullName: '', nodes: '', carbon: '', sulphur: '', mnasd: '', pasd: '', sasd: '', price: '', } this.dataSourceFeWater.splice(key + 1, 0, item) this.$set(this.editableDataFeWater, key + 1, item) },
时间: 2023-12-22 16:03:25 浏览: 36
另外一种实现方式是使用类型检查和类型转换函数,例如:
```
function isNumber(val) {
if (typeof val === 'number' && !isNaN(val)) {
return true
}
return false
}
function toNumber(val) {
if (typeof val === 'number') {
return val
}
if (typeof val === 'string') {
return Number(val)
}
return 0
}
function addItemFeWater(key) {
let item = {
key: key + 1,
abbreviation: '',
fullName: '',
nodes: '',
carbon: '',
sulphur: '',
mnasd: '',
pasd: '',
sasd: '',
price: '',
}
this.dataSourceFeWater.splice(key + 1, 0, item)
// 类型检查和转换
if (!isNumber(item.price)) {
item.price = toNumber(item.price)
}
this.$set(this.editableDataFeWater, key + 1, item)
}
```
这样,在每次添加新的item对象时,会自动将price属性的值初始化为空字符串,并且在使用item对象时,会先进行类型检查和类型转换,确保price属性的值为Number类型。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)