Vue.directive('my-directive', {
bind: function(){
//做绑定的准备工作
//比如添加事件监听器,或是其他只需要执行一次的复杂操作
},
inserted: function(){
//...
},
update: function(){
//根据获得的新值执行对应的更新
//对于初始值也会调用一次
},
componentUpdated: function(){
//...
},
unbind: function(){
//做清理操作
//比如移除bind时绑定的事件监听器
}
})
Vue.directive('my-directive', function(){
//...
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input v-focus>
</div>
<script>
//注册一个全局自定义指令v-focus
Vue.directive('focus', {
// 当绑定元素插入到DOM中
inserted: function (el) {
// 聚焦元素
el.focus()
}
});
var app = new Vue({
el: '#app'
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="hook-arguments-example" v-demo-directive:red="message"></div>
<script>
Vue.directive('demoDirective', {
bind: function(el, binding, vnode){
el.style.color = '#fff'
el.style.backgroundColor = binding.arg
el.innerHTML =
'指令名name - ' + binding.name + '<br>' +
'指令绑定值value - ' + binding.value + '<br>' +
'指令绑定表达式expression - ' + binding.expression + '<br>' +
'传入指令的参数argument - ' + binding.arg + '<br>'
},
});
var demo = new Vue({
el: '#hook-arguments-example',
data: {
message: 'hello!'
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="hook-arguments-example" v-demo-directive="{ color: 'white', text: 'hello!' }"></div>
<script>
Vue.directive('demoDirective', function(el, binding, vnode){
console.log(binding.value.color);
console.log(binding.value.text);
});
var demo = new Vue({
el: '#hook-arguments-example'
})
</script>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有