Vue 3 Can I get a list of custom directives applied to the component?

Viewed 349

Can I get a list of custom directives applied to a component? In the instance returned by getCurrentInstance call, directives are null for current component. Expected 'highlight' How can i get list of directives applied to current component? (Vue v3.1.2)

In Vue 2.x it was in this.$vnode.data.directives

https://jsfiddle.net/6e08styL/27/

html

<script src="https://unpkg.com/vue@3"></script>

<div id="app"></div>

js

const {
  createApp,
  getCurrentInstance
} = Vue

const app = createApp({
  template: `<test v-highlight="'red'"></test>`,
})

app.component('test', {
  name: 'test',
  template: `<div>{{msg}}</div>`,
  data: () => ({
      msg: 'Hello Vue.js'
  }),
  setup() {
      const instance = getCurrentInstance()
      console.log(instance)
  }
})

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

app.mount('#app')

console console log

1 Answers

It's available from:

getCurrentInstance().vnode.dirs

demo

Related