我还尝试了typeof item.type === '<type>'
instanceof
none,它的工作效果与预期一致。
背景:我正在构建一个表单生成器(使用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.<item>.type
。参数接收正常。要提供正确的输入类型,我需要知道form item
类型是什么!
我有以下方法(从谷歌搜索中的一个):
getType (elem) {
return Object.prototype.toString.call(elem).slice(8, -1).toLowerCase()
}
当我读到它的时候,它应该返回'number','date‘等等。不管我读到了什么,它也会像typeof item.type
一样返回'function’。
在表单生成器代码中,我有确定类型的方法:
...
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.fields
中的每个元素显示哪个组件。
我的其他尝试是item.type.constructor === Number
任何链接或解决方案,这是非常感谢。
发布于 2018-05-31 18:35:37
我真傻。这适用于item.type === Number
(或相关的原始数据类型)
https://stackoverflow.com/questions/50620664
复制相似问题