# vue-clipboard2

> A Vuejs2 & Vuejs3 binding for clipboard.js

Latest version **0.3.3** (published 2021-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-clipboard2
pnpm add vue-clipboard2
yarn add vue-clipboard2
bun add vue-clipboard2
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.3 |
| Published | 2021-09-15 |
| First published | 2017-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 37.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1752 |
| Author | Inndy |
| Maintainers | inndy |
| Keywords | vue, vue2, clipboard, clipboard.js |

## Links

- npm: https://www.npmjs.com/package/vue-clipboard2
- Repository: https://github.com/Inndy/vue-clipboard2
- Homepage: https://github.com/Inndy/vue-clipboard2#readme
- Issues: https://github.com/Inndy/vue-clipboard2/issues
- npm.io page: https://npm.io/package/vue-clipboard2

## Dependencies (1)

- [clipboard](https://npm.io/package/clipboard.md) ^2.0.0

## Recent versions

- 0.3.3 (latest) — 2021-09-15
- 0.3.2 — 2021-09-10
- 0.3.1 — 2019-09-02
- 0.3.0 — 2019-02-11
- 0.2.1 — 2018-07-25
- 0.2.0 — 2018-06-14
- 0.1.1 — 2018-05-17
- 0.1.0 — 2018-03-19
- 0.0.9 — 2018-03-02
- 0.0.8 — 2017-11-16
- 0.0.7 — 2017-11-08
- 0.0.6 — 2017-04-20
- 0.0.5 — 2017-04-03
- 0.0.4 — 2017-01-04
- 0.0.3 — 2017-01-04
- … 2 more at https://npm.io/package/vue-clipboard2/versions

## 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:

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

Vue.use(VueClipboard)
```

For standalone usage:

```html
<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](https://github.com/Inndy/vue-clipboard2/blob/master/samples/sample2.html),
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](https://github.com/zenorocha/clipboard.js/issues/218) and
[this page](https://github.com/zenorocha/clipboard.js/wiki/Known-Limitations) first.

## It doesn't work with bootstrap modals

See [clipboardjs](https://clipboardjs.com/#advanced-usage) document and [this pull request](https://github.com/Inndy/vue-clipboard2/pull/23), `container` option is available like this:

```js
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:

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

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

## Sample

```html
<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

```html
<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```](https://vuejs.org/v2/api/#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](https://github.com/Inndy/vue-clipboard2/blob/master/LICENSE)

---
_Source: https://npm.io/package/vue-clipboard2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
