環境vue + elemntUI相關代碼https://codepen.io/eeeecw/pen...點擊預覽具體情景在輸入框的 change 事件里,我修改了輸入框的雙向幫定值,但不是每次都顯示在輸入框里。比如上面代碼,輸入框不支持小數,每次輸入后,我都會在 change 事件里把數字轉成整數,但是顯示的值還是原來的值但是如果在 change 事件里寫一個 setTimeout 去把數字轉成整數,就回成功把最新值渲染出來。。。這是為什么?求原理?或者有什么更好的解決辦法?
3 回答

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
應為num1初始值是1,你輸入1.3655后,通過Math.floor(this.value.num1)計算之后還是num1還是1,el-input-number內部的watch沒有監聽到變化,el-input-number相關代碼如下:
watch: { value: { immediate: true, handler: function handler(value) { var newVal = value === undefined ? value : Number(value); if (newVal !== undefined) { if (isNaN(newVal)) { return; } if (this.precision !== undefined) { newVal = this.toPrecision(newVal, this.precision); } } if (newVal >= this.max) newVal = this.max; if (newVal <= this.min) newVal = this.min; this.currentValue = newVal; this.$emit('input', newVal); } } },
添加回答
舉報
0/150
提交
取消