Did VueJS remove params & paramWatchers in Vue 2?

53 Views Asked by At

I accidentally found this documentation of Vue v1, which had a feature to watch attributes on the element that the directive is bound to :

<div v-example v-bind:a="someValue"></div>

Vue.directive('example', {
  params: ['a'],
  paramWatchers: {
    a: function (val, oldVal) {
      console.log('a changed!')
    }
  }
})

I am building a custom directive library using Vue 2.7. and I was looking for a similar feature, but it's now missing in the latest documentation.

Anyway I have implemented a different logic using update hook where I need to programmatically extract attributes from the element.

const myDirective: DirectiveOptions = {
  update (el: any, binding: any) {
    const customAttribute = el.getAttribute('customAttribute')
    doSomething(customAttribute)
  }
}

But it's not perfectly match with the requirement as the update hook is triggering unnecessarily with other elements under the same parent. So this function executes multiple times unnecessarily and also I need to do some hacks to keep state of the directive.

My question is: Is this feature still available somewhere else now? or did Vue introduce a different feature to achieve the same requirement?

Please note that my project is just a set of Directives only, so it doesn't have access to components or root level to place a regular watchers to check for state changes.

0

There are 0 best solutions below