<template>
<input v-model="phone" placeholder="手机号" />
</template>
<script>
export default {
watch: {
phone(newValue, oldValue) {
// 监听
this.phone =
newValue.length > oldValue.length
? newValue
.replace(/\s/g, '')
.replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3')
: this.phone.trim();
}
}
};
</script>