最常见企业网站有哪些,微信小程序游戏排行榜,井陉县城乡建设局网站,wordpress图片上传慢一、需求说明 在项目中 点击按钮 复制 某行文本是很常见的 应用场景#xff0c;
在 Vue 项目中实现 复制功能 需要借助 vue-clipboard2 插件。
二、代码实现 1、安装 vue-clipboard2 依赖 #xff08; 出现错误的话#xff0c;可以试试切换成淘宝镜像源
npm config set r…一、需求说明 在项目中 点击按钮 复制 某行文本是很常见的 应用场景
在 Vue 项目中实现 复制功能 需要借助 vue-clipboard2 插件。
二、代码实现 1、安装 vue-clipboard2 依赖 出现错误的话可以试试切换成淘宝镜像源
npm config set registry https://registry.npm.taobao.org
npm install --save vue-clipboard2
2、在 main.js 文件中全局引入插件 示例代码如下
import Vue from vue import VueClipBoard from vue-clipboard2 Vue.use(VueClipBoard) new Vue({ render: h h(App) }).$mount(#app)
3、案例
在组件中有两种方法可以实现复制功能。
方法一
使用 vue-clipboard2 提供的 指令
直接将变量内容复制至剪切板暂时没有找到处理数据后再复制的方式
template div classyeluosen input typetext v-modelmessage el-button iconel-icon-share sizemini stylefont-size: 16px;padding: 4px 8px; title共享 v-clipboard:copyscope.row.url v-clipboard:successonCopy v-clipboard:erroronError clickshare(scope.row.url)/el-button /div /template
复制时通过 v-clipboard: copy 复制输入的内容
// 复制成功 or 失败提示信息 onCopy: function (e) { console.log(复制成功, e) }, onError: function (e) { console.log(复制失败, e) }
方法二 使用 vue-clipboard2 全局绑定的 $copyText 事件方法
复制动作使用的是 Vue 响应函数方式这就为复制前控制数据提供了可能
// 点击事件 share(val) { this.handleData(val) this.$copyText(this.message).then(function (e) { alert(Copied) }, function (e) { alert(Can not copy) }) }, // 数据处理 handleData(val){ this.message this.message val }
template div el-button typesuccess sizesmall stylemargin-left: 10px clickonCopy 复制/el-button /div /template script export default { data() { return { text: 这是一段复制的文本, }; }, methods: { onCopy() { this.$copyText(this.pathText).then( e{ console.log(复制成功, e); }, e{ console.log(复制失败, e); } ) } } }; /script
三、项目所用
实现选中并且复制成功后带有提示信息的效果
template div el-input refaddressInput v-modeladdress :readonlytrue template slotprepend 反馈地址 /template /el-input el-button clickcopyLink(address)复制链接/el-button /div /template script export default { data() { return { address: https://www.baidu.com/, // 地址信息 }; }, methods: { // 判断是否是 IE 浏览器 isIE() { if (window.ActiveXObject || ActiveXObject in window) { return true; } else { return false; } }, // 拷贝地址 copyLink(url) { if (this.isIE()) { this.$copyText(url); this.$refs.addressInput.select(); // 实现选中效果 this.$message.success(复制成功!); } else { this.$copyText(url) .then((res) { this.$refs.addressInput.select(); // 实现选中效果 this.$message.success(复制成功!); }) .catch((err) { this.$message.error(该浏览器不支持自动复制, 请手动复制); }); } }, }, }; /script style langscss scoped/style 优化一下我想要复制一个对象需要做转义像这样
el-dialog title提示 :visible.syncdialogVisible width30% :before-closehandleClosespan slotfooter classdialog-footerspan{{ form.address }}/spanspan{{ form.name }}/spanspan{{ form.password }}/spanel-button clickdialogVisible false取 消/el-buttonel-button typeprimary clickdialogVisible false确 定/el-buttonel-button typeprimary clickshare(form)复制/el-button/span/el-dialog data(){
return{form: {address: https://www.baidu.com/, // 地址信息name: 张三,password: 123,},
}
} share(url) {if (this.isIE()) {this.$copyText(this.form.password);// this.$refs.addressInput.select(); // 实现选中效果this.$message.success(复制成功!);} else {//此处做转义并且使用JSON.stringify转一下let obj {地址: this.form.address,用户名: this.form.name,密码: this.form.password}const objectString JSON.stringify(obj);this.$copyText(objectString).then((res) {// this.$refs.addressInput.select(); // 实现选中效果this.$message.success(复制成功!);}).catch((err) {this.$message.error(该浏览器不支持自动复制, 请手动复制);})}},
最终结果为{地址:https://www.baidu.com/,用户名:张三,密码:123}winv剪贴板上也会存在