官方文档:
https://cn.vuejs.org/v2/guide/custom-directive.html
全局指令
<body>
<div id="app">
<input type="text" v-focus>
<input type="text" v-bcolor="{bcolor:'aqua'}">
</div>
</body>
<script>
Vue.directive("focus", {
inserted: function (el) {
el.focus();
}
});
Vue.directive("bcolor", {
inserted: function (el, binding) {
el.style.backgroundColor = binding.value.bcolor;
}
})
var vm = new Vue({
el: "#app"
})
</script>
局部指令
<body>
<div id="app">
<input type="text" v-focus>
</div>
</body>
<script>
var vm = new Vue({
el: "#app",
directives: {
focus: {
inserted: function (el) {
el.focus();
}
}
}
});
</script>