vue2.0和vue3.0自定义指令的用法

html

<div v-setcolor="red" ></div>

1.vue2.0写法

Vue.directive('setcolor', {
   bind(el, binding, vnode)  {
      el.style.color = binding
   }
})

2.vue3.0写法(beforeMount为vue3.0的生命周期)

const app = Vue.createApp({})

app.directive('setcolor', { beforeMount(el, binding, vnode) { el.style.color = binding.value } })

  

posted @ 2020-10-27 19:12  阿凡达的小驴  阅读(846)  评论(0)    收藏  举报