Vuejs 2.0:如何在自定义指令中的钩子函数中访问vm实例?

Pra*_*GPz 2 vue.js vuejs2

在Vuejs 1.0中,它能够访问vue实例,this.vm但我无法在vue 2.0中找到这样做的方法.

我实际上正在尝试编写一个自定义指令来更新绑定数据,当一个元素由一个不触发任何更新的jquery插件更改时v-model.

`<input id="dayparting_switch" 
  v-model="options.dayparting" 
  v-observe="options.dayparting" 
  :cheked="options.dayparting" 
data-off="Disabled" data-on="Enabled" data-toggle="toggle" type="checkbox">`
Run Code Online (Sandbox Code Playgroud)
Vue.directive('observe', {
    bind(el, args) {
        var vm = this.vm;
        $(el).change(function() {
            vm.$data = 'changed';
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

Sri*_*mam 7

我看到你想访问呈现指令的组件.

Vue.directive('observe', {
  // in the bind function, the 3rd argument is vnode (the VDOM) created by Vue.
  bind(el, bindings, vnode) {
   // vnode.context is the scope where the directive is rendered.
   const vm = vnode.context 
   $(el).change(function() {
     vm.$data = 'changed';
   });
  }
});
Run Code Online (Sandbox Code Playgroud)

在源代码VNode上下文中检查此行.