Vue自定义指令
某些情况仍然需要对普通 DOM 元素进行底层操作,这时候就会用到自定义指令。
比如:获取文本框的焦点(autofocus有兼容性问题,vue中默认无法使用,需要用到表单的focus方法,vue推荐将此方法封装成自定义指令)
自定义指令
1 2 3 4 5 6 7 8 9 10 11
|
Vue.directive('focus', { inserted: function (el) { el.focus() } })
|
全局指令与局部指令
1 2 3 4 5
| Vue.directive('focus', { inserted(el) { el.focus() } })
|
1 2 3 4 5 6 7 8 9
| const vm = new Vue({ directives: { focus: { inserted(el) { el.focus() } } } })
|
指令的钩子函数
bind: 只会调用一次,当指令绑定到当前元素上时调用inserted: 被绑定元素插入父节点时调用update: 指令的值发生改变的时候componentUpdated: 指令所在的组件中所有的DOM都更新完成的时候unbind:只调用一次,指令与元素解绑时调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| Vue.directive('focus', { bind (el) { }, inserted (el) { el.focus() }, update () {
}, componentUpdated () {
}, unbind () {
}
})
|
钩子函数的参数
所有的钩子函数两个参数el和binding
1 2
| v-指令名:指令参数.指令修饰符.指令修饰符 = "指令的值" v-on:click.enter.prevent = "clickFn"
|
1 2 3 4 5 6 7 8
| el: 当前元素 binding:一个对象,包含以下属性: name:指令名,不包括 v- 前缀。 value:指令的绑定值,例如:v-my-directive="1 + 1" 中,绑定值为 2。 oldValue:指令绑定的前一个值,仅在 update 和 componentUpdated 钩子中可用。无论值是否改变都可用。 expression:字符串形式的指令表达式。例如 v-my-directive="1 + 1" 中,表达式为 "1 + 1"。 arg:传给指令的参数,可选。例如 v-my-directive:foo 中,参数为 "foo"。 modifiers:一个包含修饰符的对象。例如:v-my-directive.foo.bar 中,修饰符对象为 { foo: true, bar: true }。
|
指令的简写
如果配置的钩子函数 bind 和 update 的逻辑是一模一样的,可以这样简写:Vue.directive(指令名, 函数)
1 2 3
| Vue.directive('bgcolor', function (el, binding) { el.style.backgroundColor = binding.value })
|