浅谈vux之x-input使用以及源码解读

2020-06-14 06:22:15易采站长站整理

this.$refs.group.$children.forEach(child => {
child.reset()
})
})
}

方法二: 其实想做的就是在reset方法执行之前将currentValue置为空


created(){
this.currentValue =
this.value === undefined || this.value === null
? ''
: this.mask ? this.maskValue(this.value) : this.value
},
props:{
value: [String, Number]},
watch:{
value(val) {
this.currentValue = val
}
}

可以通过传入value来改变currentValue的值,将v-model=”name”绑定值的方式改为:value=”name”


onSubmitClick() {
this.name = ''
this.$nextTick(() => {
this.$refs.group.$children.forEach(child => {
child.reset()
})
})
}

场景2

当我们点击提交时,如果有校验选项不符合规则能提示相匹配的警告


data(){
return {
message: '还未填写信息'
}
}

将message提示信息初始值设置为还未填写信息,当我们未进行填写信息的时候点击提交显示。然后使用on-change函数绑定校验规则,实时更新message对应的提示语,业务逻辑如下:


onValueChange() {
// 多次使用赋值给变量
const children = this.$refs.group.$children
let statusList = [] // 筛选出有值的,作为是否全部未填的判断依据 如果length小于1则还没填写任何内容
statusList = children.filter(item => {
return item.currentValue
})
if (statusList.length < 1) {
this.message = '还未填写信息'
return
}
// 找到第一个没有值的那一项,如果都有则返回undefined
const firstInvalid = children.find(item => {
return !item.valid
})
if (firstInvalid !== undefined) {
this.message = `请填写正确的${firstInvalid.title}`
}
// 显示的将是否有效赋值给valid增加代码可读性
this.valid = Boolean(firstInvalid)
}

github:代码地址