vue限制input只能输入正整数
解决方法:
使用 directives自定义vue标签v-positive-int来监听输入框的值
<input v-positive-int type="text" />
export default{
name: 'Txt',
data(){
return {}
},
computed: {
},
methods: {
},
directives: {
positiveInt: {
bind: function(el) {
el.handler = function() {
el.value = Number(el.value.replace(/\D+/, ''))
}
el.addEventListener('input', el.handler)
},
unbind: function(el) {
el.removeEventListener('input', el.handler)
}
}
},
components: {
},
}