<template>
  <view>
    <view class="content">
      <view class="text-to-copy" id="copy-text">{{ textToCopy }}</view>
      <button @click="copyText">复制文本</button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';

const textToCopy = ref('这是要复制的文本');

const copyText = async () => {
  try {
    
    await uni.setClipboardData({
      data: textToCopy.value,
      success: () => {
        uni.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 2000
        });
      },
      fail: (err) => {
        uni.showToast({
          title: '复制失败',
          icon: 'none',
          duration: 2000
        });
      }
    });
  } catch (error) {
    console.error('复制失败:', error);
    uni.showToast({
      title: '复制失败',
      icon: 'none',
      duration: 2000
    });
  }
};
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.text-to-copy {
  font-size: 16px;
  margin-bottom: 10px;
}
</style>