A
A
Aleksandr_KH2019-07-29 14:12:37
Vue.js
Aleksandr_KH, 2019-07-29 14:12:37

How to create a directive on VUE?

Hello.
Guys, tell me how to make a directive out of this ?
To make it look something like this
<div v-clipboard="someData">Copy button</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-07-29
@Aleksandr_KH

how to make a directive out of this ?

That's right - no way. You don't need to use execCommand :
Note: This method is obsolete and should not be used. In particular, to interact with the clipboard, consider using the Clipboard API instead.

So let's follow the advice from the documentation, and use modern tools:
const clipboardDirective = (() => {
  const values = new Map();
  const onClick = e => navigator.clipboard.writeText(values.get(e.currentTarget));

  return {
    bind(el, binding) {
      el.addEventListener('click', onClick);
      values.set(el, binding.value);
    },
    update(el, binding) {
      values.set(el, binding.value);
    },
    unbind(el) {
      el.removeEventListener('click', onClick);
      values.delete(el);
    },
  };
})();

Vue.directive('clipboard', clipboardDirective);

Well, okay?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question