uniapp 复制内容到剪切板

js yekong

关键代码

uni.setClipboardData({
    data:value,//要被复制的内容
    success:()=>{//复制成功的回调函数
      uni.showToast({//提示
        title:'复制成功'
      })
    }
});

组件封装

/**
* @Author: 858834013@qq.com
* @Name: setClipboardData
* @Date: 2022-10-21
*/
<template>
	<div @click="getCopy">
		<slot></slot>
	</div>
</template>

<script>
	export default {
		name: "setClipboardData",
		props: {
			data: {
				type: String,
				default () {
					return '';
				}
			},
		},
		methods: {
			getCopy() {
				uni.setClipboardData({
					data: this.data, 
					success: () => {
						uni.showToast({
							title: '复制成功'
						})
					}
				});
			},
		}
	}
</script>

使用实例

<setClipboardData :data="data.order_number">
	<div class="copy">复制</div>
</setClipboardData>
喜欢