vue.js3:使用clipboard.js实现复制到剪贴板(vue@3.2.37 / clipboard@2.0.11)
一,安装clipboard.js
1,官网:
https://clipboardjs.com/
代码地址
https://github.com/zenorocha/clipboard.js
在npmjs的地址
https://www.npmjs.com/package/clipboard
2,安装
liuhongdi@lhdpc:/data/vue/axios$ npm install clipboard --save added 5 packages in 3s
3,查看已安装的库的版本
liuhongdi@lhdpc:/data/vue/axios$ npm list clipboard axios@0.1.0 /data/vue/axios └── clipboard@2.0.11
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/06/02/vue-js3-shi-yong-clipboard-js-shi-xian-fu-zhi-dao-jian-tie/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,js代码
html:
<div style="width:800px;margin: auto;"> <el-input v-model="rgb" placeholder="Please input" readonly="readonly"> <template #prepend >RGB</template> <template #append> <el-icon class="copyColor" @click="copy('rgb')" style="vertical-align: middle;"> <CopyDocument /> </el-icon> </template> </el-input> <el-input v-model="hex" placeholder="Please input" readonly="readonly" style="margin-top: 10px;"> <template #prepend>十六进制</template> <template #append> <el-icon class="copyColor" @click="copy('hex')" style="vertical-align: middle;"> <CopyDocument /> </el-icon> </template> </el-input> </div>
js:
import Clipboard from 'clipboard' ... //复制颜色值到剪贴板 const copy = (type) => { let clipboard = new Clipboard('.copyColor', { text: function () { //返回要复制的文本 if (type == 'rgb') { return rgb.value; } else { return hex.value; } } }) clipboard.on('success', () => { ElMessage.success('复制成功'); //释放内存 clipboard.destroy() }) clipboard.on('error', () => { ElMessage.error('复制失败'); //释放内存 clipboard.destroy() }) }
三,测试效果
四,查看vue.js框架的版本:
liuhongdi@lhdpc:/data/vue/child$ npm list vue child@0.1.0 /data/vue/child ├─┬ @vue/cli-plugin-babel@5.0.6 │ └─┬ @vue/babel-preset-app@5.0.6 │ └── vue@3.2.37 deduped └─┬ vue@3.2.37 └─┬ @vue/server-renderer@3.2.37 └── vue@3.2.37 deduped