【问题标题】:Retrieve data's component from custom directive从自定义指令中检索数据的组件
【发布时间】:2018-08-02 06:38:30
【问题描述】:

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

<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

【问题讨论】:

    标签: vue.js


    【解决方案1】:

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

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

    Reference

    【讨论】:

    • 非常感谢您!我在文档中提出了这一点:/
    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2019-04-04
    • 1970-01-01
    • 2015-09-10
    相关资源
    最近更新 更多