从自定义指令中检索数据的组件

时间:2018-08-02 06:38:30

标签: vue.js

所以我想使用指令绑定样式,因为我需要测试发送到元素的值。

<div v-portrait="photo.portrait"></div>

问题是我需要访问组件的data(),但由于this超出范围,我不知道如何。

directives: {
    portrait: {
        bind: function (el, binding) {
            if(binding.value == true){
                console.log(this); //undefined
                el.height = (this.rowHeight)+ 'px';
            }
        }
    }
}

那我怎么能找回this.rowHeight

1 个答案:

答案 0 :(得分:1)

bind有第三个参数,它是vnode。您可以通过此参数访问Vue实例

bind: function (el, binding, vnode) {
  if(binding.value == true){
    console.log(this); //undefined
    el.height = (vnode.context.rowHeight)+ 'px';
  }            
 }

Reference