Javascript:确定作为参数传递时原语的类型
已尝试Javascript:确定作为参数传递时原语的类型,javascript,types,parameters,Javascript,Types,Parameters,已尝试typeof item.type==''也尝试instanceof没有按预期工作 背景:我正在构建一个表单生成器(使用VueJS) 这是表单定义: formDesc: { inline: true, columns: 2, labels: 'top', disabled: false, fields: [ {type: Array, prop: 'aprovalStatus', list:
typeof item.type==''
也尝试instanceof
没有按预期工作
背景:我正在构建一个表单生成器(使用VueJS)
这是表单定义:
formDesc: {
inline: true,
columns: 2,
labels: 'top',
disabled: false,
fields: [
{type: Array, prop: 'aprovalStatus', list: ['Pending', 'Rejected', 'Conditional', 'Approved'], label: 'Status', required: true},
{type: String, prop: 'remarks', html: true, label: 'Remarks'},
{type: Date, prop: 'approvalDate', label: 'Approval Date', format: 'DD-MM-YYYY'},
{type: Number, prop: 'deposit', decimals: 2, label: 'Security Deposit'}
]
}
我遇到的问题是确定表单生成器端的form.fields..type
。参数接收良好。要提供正确的输入类型,我需要知道表单项的类型是什么
我有以下方法(来自一个谷歌搜索):
正如我读到的,应该返回“数字”、“日期”等。
不管我读到了什么,它也会返回'function',就像typeof item.type
一样
在表格生成器代码中,我有确定类型的方法:
...
check (item) {
if (item && item.type) {
console.log('getType = ', this.getType(item.type))
return true
} else {
this.$message({message: 'Field item problem [' + !item ? 'Empty item' : 'no type prop for: ' + (item && item.label ? item.label : 'No label') + ']', type: 'warning'})
return false
}
},
isArray (item) {
if (this.check(item) && this.getType(item.type) === 'array' &&
item.hasOwnProperty('list')) {
return true
} else {
return false
}
},
...
isArray、isNumber、is Decimal
等用于确定在formDesc.字段中为每个元素显示哪个组件
我的其他尝试是item.type.constructor==Number
非常感谢您为此提供的任何链接或解决方案。愚蠢的我。这适用于item.type==Number
(或相关的基本数据类型)
...
check (item) {
if (item && item.type) {
console.log('getType = ', this.getType(item.type))
return true
} else {
this.$message({message: 'Field item problem [' + !item ? 'Empty item' : 'no type prop for: ' + (item && item.label ? item.label : 'No label') + ']', type: 'warning'})
return false
}
},
isArray (item) {
if (this.check(item) && this.getType(item.type) === 'array' &&
item.hasOwnProperty('list')) {
return true
} else {
return false
}
},
...