前言
為了提高組件的復用性,所以利用插件vue-clipboard2,封裝組件CopyDialog以實現一鍵復制文本到剪貼板的功能。
安裝
npm install --save vue-clipboard2
插件的使用(引入)
import Vue from 'vue' import VueClipboard from 'vue-clipboard2' Vue.use(VueClipboard)
組件的封裝 copyDialog.vue
1 <template> 2 <div> 3 <slot name="content"></slot> 4 <span class="copyContent" v-show="info" @click="$_copyInfo(info)">復制</span> 5 </div> 6 </template> 7 8 <script> 9 export default { 10 name: 'copyDialog', 11 props: { 12 info: { 13 type: [Number, String], 14 default: '', 15 }, 16 }, 17 methods: { 18 $_copyInfo(info) { 19 this.$copyText(info).then(() => { 20 this.$message.success('復制成功'); 21 }, () => { 22 this.$message.error('復制失敗'); 23 }); 24 }, 25 }, 26 }; 27 </script> 28 29 <style lang="less"> 30 .copyContent { 31 cursor: pointer; 32 color: #357ef6; 33 } 34 </style>
參數的解釋:info 要復制的文本,類型是String/Number
組件的使用
import CopyDialog from 'xxx/CopyDialog'; <CopyDialog info="information"></CopyDialog>
