Vue 常用特性 (自定义指令)

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'}">

猜你喜欢

转载自blog.csdn.net/weixin_50001396/article/details/113822605