如何用vue写一个限制input输入框长度的demo呢,我试了试v-on:keydown的方法里return false是无法阻止输入值事 因为刚学vue所以思维可能还是jq的
Vue.directive('maxchars', {
bind: function () {
var self = this;
this.handler = function () {
var max_chars = self.vm.$get(self.expression)
if (this.el.value.length > max_chars) {
this.el.value = this.el.value.substr(0, max_chars)
}
}.bind(this)
this.el.addEventListener('input', this.handler)
},
unbind: function () {
this.el.removeEventListener('input', this.handler)
}
})
利用vue的过滤器filter 在控制器里面 判断长度。超过了就返回需求长度