源代码 :
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 开源教程(ossoft.cn)</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.0.5/vue.global.js"></script> </head> <body> <div id="app"> <div v-runoob="{ color: 'green', text: '开源教程!' }"></div> </div> <script> const app = Vue.createApp({}) app.directive('runoob', (el, binding, vnode) => { el.innerHTML = binding.value.text el.style.backgroundColor = binding.value.color }) app.mount('#app') </script> </body> </html>
运行结果