vue-clipboard2 vs vue-clipboard3
剪贴板操作
vue-clipboard2vue-clipboard3

剪贴板操作

剪贴板操作库允许开发者在Web应用中轻松实现复制和粘贴功能。这些库通常提供简单的API来将文本或其他数据复制到用户的剪贴板,或者从剪贴板中读取数据。它们在实现如复制按钮、粘贴功能或处理剪贴板数据时非常有用。vue-clipboard2是一个为Vue.js设计的轻量级剪贴板库,支持复制文本和处理剪贴板事件。vue-clipboard3vue-clipboard2的升级版,基于Vue 3构建,提供更好的性能和更现代的API,支持Composition API和更灵活的剪贴板操作。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
vue-clipboard201,753-375 年前MIT
vue-clipboard3014312.5 kB12-MIT

功能对比: vue-clipboard2 vs vue-clipboard3

Vue版本支持

  • vue-clipboard2:

    vue-clipboard2支持Vue 2,适合使用Vue 2.x的项目。

  • vue-clipboard3:

    vue-clipboard3专为Vue 3设计,利用了Vue 3的新特性,适合使用Vue 3.x的项目。

API设计

  • vue-clipboard2:

    vue-clipboard2提供简单直观的API,易于使用,特别是对于需要快速实现复制功能的开发者。

  • vue-clipboard3:

    vue-clipboard3提供更现代化的API,支持Composition API,允许开发者以更灵活的方式使用剪贴板功能。

性能

  • vue-clipboard2:

    vue-clipboard2性能良好,适合大多数常规使用场景。

  • vue-clipboard3:

    vue-clipboard3在性能上进行了优化,特别是在处理大量剪贴板操作时,表现更佳。

文档和社区支持

  • vue-clipboard2:

    vue-clipboard2拥有良好的文档和活跃的社区,支持Vue 2的开发者可以轻松找到使用示例和解决方案。

  • vue-clipboard3:

    vue-clipboard3作为新一代库,文档清晰,社区正在快速增长,特别是在Vue 3开发者中。

代码示例

  • vue-clipboard2:

    vue-clipboard2代码示例

    <template>
      <div>
        <button v-clipboard:copy="text" v-clipboard:success="onSuccess">复制文本</button>
      </div>
    </template>
    <script>
    export default {
      data() {
        return {
          text: '要复制的文本'
        };
      },
      methods: {
        onSuccess() {
          alert('文本已成功复制!');
        }
      }
    };
    </script>
    
  • vue-clipboard3:

    vue-clipboard3代码示例

    <template>
      <div>
        <button @click="copyText">复制文本</button>
      </div>
    </template>
    <script>
    import { ref } from 'vue';
    import { useClipboard } from 'vue-clipboard3';
    export default {
      setup() {
        const { copy } = useClipboard();
        const text = ref('要复制的文本');
        const copyText = () => {
          copy(text.value);
          alert('文本已成功复制!');
        };
        return { copyText };
      }
    };
    </script>
    

如何选择: vue-clipboard2 vs vue-clipboard3

  • vue-clipboard2:

    如果您正在使用Vue 2并需要一个简单易用的剪贴板库,选择vue-clipboard2。它轻量级,易于集成,适合需要基本剪贴板功能的项目。

  • vue-clipboard3:

    如果您正在使用Vue 3并希望利用Composition API和更现代的特性,选择vue-clipboard3。它提供更好的性能和灵活性,适合需要更复杂剪贴板操作的应用。

vue-clipboard2的README

vue-clipboard2

A simple vuejs 2 binding for clipboard.js

Install

npm install --save vue-clipboard2 or use dist/vue-clipboard.min.js without webpack

Usage

For vue-cli user:

import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'

Vue.use(VueClipboard)

For standalone usage:

<script src="vue.min.js"></script>
<!-- must place this line after vue.js -->
<script src="dist/vue-clipboard.min.js"></script>

I want to copy texts without a specific button!

Yes, you can do it by using our new method: this.$copyText. See sample2, where we replace the clipboard directives with a v-on directive.

Modern browsers have some limitations like that you can't use window.open without a user interaction. So there's the same restriction on copying things! Test it before you use it. Make sure you are not using this method inside any async method.

Before using this feature, read: this issue and this page first.

It doesn't work with bootstrap modals

See clipboardjs document and this pull request, container option is available like this:

let container = this.$refs.container
this.$copyText("Text to copy", container)

Or you can let vue-clipboard2 set container to current element by doing this:

import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'

VueClipboard.config.autoSetContainer = true // add this line
Vue.use(VueClipboard)

Sample

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

<template id="t">
  <div class="container">
    <input type="text" v-model="message">
    <button type="button"
      v-clipboard:copy="message"
      v-clipboard:success="onCopy"
      v-clipboard:error="onError">Copy!</button>
  </div>
</template>

<script>
new Vue({
  el: '#app',
  template: '#t',
  data: function () {
    return {
      message: 'Copy These Text'
    }
  },
  methods: {
    onCopy: function (e) {
      alert('You just copied: ' + e.text)
    },
    onError: function (e) {
      alert('Failed to copy texts')
    }
  }
})
</script>

Sample 2

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

  <template id="t">
    <div class="container">
    <input type="text" v-model="message">
    <button type="button" @click="doCopy">Copy!</button>
    </div>
  </template>

  <script>
  new Vue({
    el: '#app',
    template: '#t',
    data: function () {
      return {
        message: 'Copy These Text'
      }
    },
    methods: {
      doCopy: function () {
        this.$copyText(this.message).then(function (e) {
          alert('Copied')
          console.log(e)
        }, function (e) {
          alert('Can not copy')
          console.log(e)
        })
      }
    }
  })
  </script>

You can use your Vue instance vm.$el to get DOM elements via the usual traversal methods, e.g.:

this.$el.children[1].children[2].textContent

This will allow you to access the rendered content of your components, rather than the components themselves.

Contribution

PRs welcome, and issues as well! If you want any feature that we don't have currently, please fire an issue for a feature request.

License

MIT License