2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VueでClipboardにコピーできるようにする

Posted at

やりたいこと

  • VueでClipboardにコピーできるようにする

前提

  • Vue 3.10

対応策

手順

  • インストール
    • npm install --save vue-clipboard2
  • main.jsに以下を記載する
import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'

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> 
  • Sample2
<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>

補足

  • Cordova + Vue のアプリでiOS、Android実機で動作を確認済み
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?