記一次el-input使用的坑
el-input使用不同與原生input,所以在vue中改變綁定的數據時需注意
<el-input v-model="form.schedule" @input="validateNumber($event)" />
要想在input時改變form.schedule的值來改變輸入框顯示的值,以下方法是做不到的
//只可以輸入1-7之間的數字
validateNumber(value) {if (!/[^1-7]/.test(value)) {this.form.schedule = value;} else {this.form.schedule = value.substring(0, value.length - 1);}
}
雖然改變了form.schedule的值,但是輸入框顯示的內容只會改變一次,然后顯示的值與綁定的值無關,即輸入什么顯示什么,不再顯示form.schedule的值
需要使用 this.$nextTick()
//驗證班期的有效輸入
validateNumber(value) {if (!/[^1-7]/.test(value)) {this.$nextTick(() => {this.form.schedule = value;});} else {this.$nextTick(() => {this.form.schedule = value.substring(0, value.length - 1);});}
}
搞定!