安装:

1 npm install --save vue-clipboard2

在main.js中引入

1 import VueClipboard from 'vue-clipboard2';
2 Vue.use(VueClipboard);

例子:

第一种:

1        <div>
2           <span class="key-box">{{ key }}</span>
3           <el-button class="but"
4             type="primary"
5             size="small"
6             v-clipboard:copy="key"
7             v-clipboard:success="onCopy"
8             v-clipboard:error="onError">复制</el-button>
9         </div>
1    onCopy() {
2       this.$message({
3         message: '复制成功',
4         type: 'success'
5       });
6     },
7     onError() {
8       this.$message.error('复制失败');
9     }

 第二种:

<div ></div>

  <template >
    <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>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2021-04-30
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
相关资源
相似解决方案