H
H
hannyto11112021-06-27 09:13:54
Vue.js
hannyto1111, 2021-06-27 09:13:54

How to put method in Vue 3 directive?

Hello, I need to write a specific global directive that should perform the same function in different hooks.
Example

directives: {
    widthAsChild: {
      methods: {
        widthAsChild(el) {
          el.style.width = getComputedStyle(el.firstChild).getPropertyValue(
            "width"
          );
        },
      },
      mounted(el) {
        this.widthAsChild(el);
        window.addEventListener("resize", () => {
          this.widthAsChild(el);
        });
      },
    },
  },
is it possible to implement something like this only in the directive without going to Ctrl-C + Ctrl-V and without evicting the method from under the directive, the directive is global and it is a bad idea to prescribe the method in each component, as an option, you can put the method in the global mixin, but then it will not be so it is clear why such a method is needed

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question