全局作用
Vue.directive('haha',{inserted:function(elment){elment.style.color = 'red';}});
局部作用
directives:{haha:{inserted: function (elment) {elment.style.color = 'red';}}}
HTML
<!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>Document</title><script src="./vue.js"></script>
</head><body><div id="app"><p v-bind:class="msg"></p><p v-haha>123hahaha</p></div>
</body>
<script>// 定义一个全局指令/* Vue.directive('haha',{inserted:function(elment){elment.style.color = 'red';}}); */var app = new Vue({el: '#app',data: {msg:'hello'},methods: {},// 局部自定义指令directives:{haha:{inserted: function (elment) {elment.style.color = 'red';}}}});
</script></html>