vue 指令directive如何接受多个参数?

假设我有一个只能输入数字的input,并且长度和小数点后位数有限制,那么我就需要两个参数了,我该如何传递和指令里面获取呢?

下面是我传递一个参数或获取的代码,如下:

<input type="text" v-spec-input='8'>
directives: {
    spec_input: {
        bind: function (el, binding) {
            //获取
            console.log(binding.value)
        }
    },
}        
阅读 14.7k
4 个回答

谢谢大伙,我找到了

<div v-demo="{ color: 'white', text: 'hello!' }"></div>

Vue.directive('demo', function (el, binding) {
  console.log(binding.value.color) // => "white"
  console.log(binding.value.text)  // => "hello!"
})

对象的形式传进去

<input type="text" numberLength='8' pointLength='3'>
directives: {
    spec_input: {
        bind: function (el, binding) {
            //获取
            console.log(el.attributes.numberLength.value,el.attributes.pointLength.value )
        }
    },
}      

这样应该可以。

参数以对象或数组的方式传递

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题