对普通DOM元素进行底层操作时,需要自定义指令。
template模板
<input v-focus />
自定义指令
directives: {
focus: {
// 指令的定义
inserted: function (el) {
el.focus()
}
}
}
template模板
<div v-demo="{ color: 'white', text: 'hello!' }"></div>
自定义指令
directives: {
demo: {
// 指令的定义
function (el) {
console.log(binding.value.color) // => "white"
console.log(binding.value.text) // => "hello!"
}
}
}
原文:https://www.cnblogs.com/zero-zm/p/12102713.html