首页 > 其他 > 详细

vue(2) - 自定义指令

时间:2017-08-02 20:21:05      阅读:188      评论:0      收藏:0      [点我收藏+]

我们都知道v-for、v-html、等等都是指令:扩展html 语法

自定义指令:

属性指令

Vue.deirctive(指令名称,function(){

  this.el ==>原生的dom元素

})

<div id="box">
	<span v-red>1222</span>
</div>

<script>
	Vue.directive(‘red‘,function(){
		this.el.style.background="red"
	});

	new Vue({
		el:"#box",
		data:{}				
	})
</script> 

或者用参数的方法

<div id="box">
	<span v-red="‘blue‘">1222</span>
</div>

<script>
	Vue.directive(‘red‘,function(color){
		this.el.style.background=color
	});

	new Vue({
		el:"#box",
	})
</script>  

指令名称: v-red ===> red

需要注意的必须以v-开头

 

自定义元素的指令(用处不是很大)

 

Vue.elementDirecitive(“zns-red”,{

  bind:function(){

    this.el.style.background="red";

  }

 

})

<div id="box">
	<zns-red>1222</zns-red>
</div>

<script>
	Vue.elementDirective(‘zns-red‘,{
		bind:function(){
			this.el.style.background=‘red‘
		}
		
	});

	new Vue({
		el:"#box",

	})
</script>

  

 

vue(2) - 自定义指令

原文:http://www.cnblogs.com/sun927/p/7275820.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!