![Vue.js从入门到项目实践(超值版)](https://wfqqreader-1252317822.image.myqcloud.com/cover/987/44509987/b_44509987.jpg)
上QQ阅读APP看书,第一时间看更新
3.3.3 twoWay
![](https://epubservercos.yuewen.com/CC45E5/23721548909464406/epubprivate/OEBPS/Images/Figure-P58_6475.jpg?sign=1738912296-H3Pg3bvOAw1cXLEkRTl5q1FyKLOvawvx-0-7ac49ecbf821cde49d457a9cae2787ee)
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) } })