通过单击 vuejs 组件中的按钮将 url 复制到剪贴板

mah*_*ich 8 javascript vue.js vue-component vuejs2

我有以下组件,我想要一个按钮,link_url可以在单击时将其复制到剪贴板。

我有选择 id 时工作的 javascript 代码,但是链接没有 id。我可以a-tag在组件本身中完成对via refs的选择,或者完成此操作的最佳方法是什么。

我也在考虑copyURL()动态生成一个带有 this.link_url 的标签,但我想那会很脏。我正在寻找 vuejs 的方式。

<template>
  <li class="list-group-item">
    <a :href="link_url" 
         class="text-dark" 
         target="_blank" 
         rel="noopener noreferrer">{{ link_name }}</a>
    <button @click="copyUrl">copy url from a tag</button>
  </li>      
</template>

<script>
export default {
  props: ["link_url", "link_name"],
  methods: {
    copyURL() {
      var Url = document.getElementById('myid'); /*GET vuejs el reference here (via $ref) but how?*/
      Url.innerHTML = window.location.href;
      console.log(Url.innerHTML)
      Url.select();
      document.execCommand("copy");
    }
  }
}
</script>

<style>
</style>
Run Code Online (Sandbox Code Playgroud)

Jun*_*ell 40

您可以在 JavaScript 中将导航器对象与剪贴板一起使用。

注意:navigator.clipboard.writeText 是异步的。

methods: {
  async copyURL(mytext) {
    try {
      await navigator.clipboard.writeText(mytext);
      alert('Copied');
    } catch($e) {
      alert('Cannot copy');
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这不适用于非 HTTPS 连接 (7认同)

fab*_*uex 12

如果你需要使用 vuejsref添加它作为属性

<a :href="link_url" class="text-dark" target="_blank" rel="noopener noreferrer" ref="mylink">
    {{ link_name }}
</a>
Run Code Online (Sandbox Code Playgroud)

并按以下方式在您的方法中使用它:

  methods: {
    copyURL() {
      var Url = this.$refs.mylink;
      Url.innerHTML = window.location.href;
      console.log(Url.innerHTML)
      Url.select();
      document.execCommand("copy");
    }
  }
Run Code Online (Sandbox Code Playgroud)

但是,您应该查看此链接以获得更好的跨浏览解决方案。在这种情况下,您不需要该ref属性。

这是适合您情况的链接中的解决方案:

methods: {
    copyUrl() {
        const el = document.createElement('textarea');  
        el.value = this.link_url;                                 
        el.setAttribute('readonly', '');                
        el.style.position = 'absolute';                     
        el.style.left = '-9999px';                      
        document.body.appendChild(el);                  
        const selected =  document.getSelection().rangeCount > 0  ? document.getSelection().getRangeAt(0) : false;                                    
        el.select();                                    
        document.execCommand('copy');                   
        document.body.removeChild(el);                  
        if (selected) {                                 
          document.getSelection().removeAllRanges();    
          document.getSelection().addRange(selected);   
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

大多数现代网络浏览器(2021)允许您使用:navigator.clipboard.writeText(“yourText”);

以防万一你也可以这样做:

  const clipboardData =
    event.clipboardData ||
    window.clipboardData ||
    event.originalEvent?.clipboardData ||
    navigator.clipboard;

  clipboardData.writeText(message);
Run Code Online (Sandbox Code Playgroud)