1. 为何需要自定义指令
Vue 总共十三个内置指令,但是这些指令还不够用,所以就需要自定义指令
2. 自定义指令的语法规则(获取元素焦点)
Vue.directive('focus',{
insterted:function(el){
//获取元素焦点
el.focus();
}
}
3. 自定义指令用法
<input type= "text" v-focus>
4.带参数的自定义指令(改变元素背景色)
Vue.directive('color',{
inserted:function(el,binding){
el.style.backgroundColor = binding.value.color;
}
}
5. 指令的用法
<input type ='text' v-color="{color:'orange'}">