- Vue.js從入門到項目實踐(超值版)
- 聚慕課教育研發中心編著
- 107字
- 2022-07-29 14:27:38
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) } })