import Vue from 'vue'

//text-area 限制输入字数
//v-toRemarkNum= {num:4}

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