
上QQ阅读APP看书,第一时间看更新
3.3.3 twoWay

twoWay:在自定义指令中,如果指令想向Vue实例写回数据,就需要在定义对象中使用twoWay:true。这样,该选项可以在指令中使用this.set(value)。
代码如下:
Vue.directive('example', { twoWay: true, bind: function(){ this.handler = function(){ //将数据写回vm //如果指令这样绑定v-example="a.b.c",它将用给定值设置 'vm.a.b.c' this.set(this.el.value) }.bind(this) this.el.addEventListener('input', this.handler) }, unbind: function(){ this.el.removeEventListener('input', this.handler) } })