<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>自定义指令</title>
</head>
<body>
<div id="app">
<strong v-red>红色文字</strong>
<strong v-red>红色文字2</strong>
<strong v-red>红色文字3</strong>
<input v-focus>
</div>
<script src="vue.js"></script>
<script>
//directive:是注册指令的入口
Vue.directive("red", function (el, tags) {
//console.log(a,b,c,d,e,f,g);
debugger
el.style.color = "red";
el.style.background = "green";
});
/*
//全局注册组件,当前页面都可以用到
Vue.directive('focus', {
// 当被绑定的元素插入到 DOM 中时……
inserted: function (el) {
// 聚焦元素
el.focus()
}
})*/
new Vue({
el: "#app",
data: {
},
//属于局部注册,只能在当前实例当中注册
directives: {
focus: {
// 指令的定义
inserted: function (el,binding,vnode,oldVnode) {
console.log(el);
console.log(binding);
console.log(vnode);
console.log(oldVnode);
el.focus();
}
}
}
});
</script>
</body>
</html>
如果需要更多的帮助,请关注公众号
网友评论