Vue 自定义指令是可以在 DOM 元素上添加特殊功能的一种方式。下面是实现按钮禁用功能的代码示例:
Vue.directive('disable', {
bind: function (el, binding) {
if (binding.value) {
el.setAttribute('disabled', true)
} else {
el.removeAttribute('disabled')
}
},
update: function (el, binding) {
if (binding.value) {
el.setAttribute('disabled', true)
} else {
el.removeAttribute('disabled')
}
}
})