1. 程式人生 > 實用技巧 >微信小程式實現複製文字功能

微信小程式實現複製文字功能

  1. 開啟文字複製功能
<text user-select="true">微信小程式中,使文字節點長按時,可以出現複製等選項</text>
  1. 實現點選按鈕複製:
<templete>
  <view>
  <text class="copy-btn" @click="copytext(value)" v-if="showCopyBtn">複製{{value}}</text>
  </view>
  </templete>
<script>
  
  export default {
   data() {
    return {
     	value: '複製的文字' 
    }
     
   },
    
    methods: {
        copytext(value) {

    if (typeof value === 'number') {

        uni.setClipboardData({
            data: value + '',
            success(res) {
                console.log(res)
            },
            fail(err) {
                uni.showToast({
                    title: '複製失敗',
                    icon: 'none'
                });
            }
        })
    } else {
        uni.showToast({
            title: '請先計算後複製',
            icon: 'none'
        });
    }
},
  
    }
    
    
  }

  
  </script>