如圖所示的運(yùn)行結(jié)果

344電話號碼分隔
假設(shè)我們給這個輸入框弄的v-model="tel"
<input type="tel" v-model="tel" placeholder="請輸入">
在watch中監(jiān)聽這個tel變量,首先,在字符到達(dá)3位以及8位的時候添加空格進(jìn)行分隔,然后,你就會發(fā)現(xiàn)一個bug,你刪除的時候無論如何這個空格都刪不掉了,所以,要監(jiān)聽刪除的時候的事件,用trim()將空格刪除
watch:{
tel(newValue, oldValue) {
if (newValue.length > oldValue.length) { //添加
if (this.tel.length === 3 || this.tel.length === 8) {
this.tel = this.tel + ' ';
}
} else { //刪除
if (this.tel.length === 4 || this.tel.length === 9) {
this.tel = this.tel.trim();
}
}
}
}