<body>
<h1>Vue.directive 自定义指令<h1>
<hr>
<div id="app">
<div v-jspan="color">{{num}}</div>
<p><button @click="add">ADD</button></p>
</div>
<div>
<p>
<button onclick="unbind11()">解绑</button>
</p>
</div>
<script type="text/javascript">
function unbind11(){
app.$destroy();//解除绑定
}
Vue.directive("jspan",{
bind:function(el,binding){
// console.log(el);
// console.log(binding);//对象
// console.log(binding.name);//自定义指令的名称 jspan
// console.log(binding.value);//自定义指令的值
// console.log(binding.expression);
console.log('1-bind');
el.style="color:"+binding.value;
},
inserted:function(){//绑定到节点
console.log('2-inserted');
},
update:function(){//组件更新
console.log('3-update');
},
componentUpdated:function(){//组件更新完成
console.log('4-componentUpdated');
},
unbind:function(){//解绑
console.log('5-unbind');
}
});
var app = new Vue({
el:'#app',
data:{
num:10,
color:'red',
},
methods:{
add:function(){
this.num ++;
}
}
})
</script>
</body>
网友评论