vue-clipboard2 vs vue-clipboard3
Adding a copy action to a Vue component
vue-clipboard2vue-clipboard3Similar Packages:

Adding a copy action to a Vue component

Choose between a Vue plugin with directive and instance integration and a Vue Composition API helper.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
vue-clipboard2129,0331,749-375 years agoMIT
vue-clipboard398,22614312.5 kB12-MIT

Let the Vue component style decide

vue-clipboard2 is installed with Vue.use. It supports directive-based copying and the component's $copyText method. It fits an existing Vue 2 template where copy triggers and feedback are already expressed using those conventions.

vue-clipboard3 exposes a useClipboard helper that returns toClipboard. Call it from a copy action and await it before showing success. Its documented optional container can be supplied when the component needs clipboard handling in a specific DOM container.

Migrate the user interaction as well as the import

A migration replaces directive or instance wiring with a component handler and feedback tied to the promise result. Keep the click and the copy action close together, and exercise the actual browser and component container where they run. For either package, handle copy failure separately from success so the UI reflects what happened.

How to Choose: vue-clipboard2 vs vue-clipboard3

  • vue-clipboard2:

    Choose it for an existing Vue 2 component using plugin registration, clipboard directives or the instance copy method.

  • vue-clipboard3:

    Choose it for a Vue 3 component that wants a Composition API helper with a promise result.

README for vue-clipboard2

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