import Vue from 'vue'

//input 限制输入字数
//v-toInputNum= {num:4}

Vue.directive('toInputNum', {
    // 指令的定义
    inserted: function(el, binding, vnode) {
        el.addEventListener('input', function () {
            if (binding.value) {
                checkValue(el, binding.value.num)
            } else {
                checkValue(el, 50)
            }
        })
        function checkValue(el, defu) {
            let len = el.value.length
            if (el.value && len > defu) {
                el.value = el.value.substring(0, defu);
                el.dispatchEvent(new Event("input"));//调用input事件使vue v-model绑定更新
            }
        }
    }
})
