按钮 提示 是 uview的组件
<u-button type="primary" shape="circle" text="复制连接" @click="copyConnection"></u-button>
<script>
export default {
data() {
return {
val: 'https://www.baidu.com/'
}
},
onLoad() {},
methods: {
// 复制 粘贴
copyConnection() {
console.log(this.val)
var _this = this;
// #ifndef H5
uni.getClipboardData({
success: function(res) {
console.log(res.data);
}
})
uni.setClipboardData({
data: _this.val,
success: function() {
_this.$refs.uToast.show({
message: _this.val,
type: 'success',
duration: 2000
})
}
})
// #endif
// #ifdef H5
var textarea = document.createElement("textarea")
textarea.value = _this.val
textarea.readOnly = "readOnly"
document.body.appendChild(textarea)
textarea.select()
textarea.setSelectionRange(0, _this.val.length)
document.execCommand("copy")
textarea.remove()
_this.$refs.uToast.show({
message: _this.val,
type: 'success',
duration: 2000
})
// #endif
}
}
}
</script>
uni-app复制插件 兼容安卓 、ios 和 大部分小程序,使用方便,上手简单。
使用步骤
点击下载插件 uni-app 复制插件
引入插件
import uniCopy from '@/js_sdk/xb-copy/uni-copy.js'
/**
copy 是要复制的文本内容
tip 复制成功后的提示语
*/
export default function (copy, tips) {
uniCopy({
content:copy,
success:(res)=>{
uni.showToast({
title: tips,
icon: 'none'
})
},
error:(e)=>{
uni.showToast({
title: e,
icon: 'none',
})
}
})
}
注意事项
该插件只能在点击事件中生效,不能在异步和回调中生效。