微信小程序在一些特定情況下是需要用戶可以長(zhǎng)按復(fù)制或者點(diǎn)擊復(fù)制一些文字或者數(shù)據(jù)的,例如商城的快遞單號(hào)等,這個(gè)時(shí)候就需要我們使用小程序api來(lái)實(shí)現(xiàn)這個(gè)功能。
實(shí)現(xiàn)上述內(nèi)容,就是使用微信設(shè)置剪貼板內(nèi)容api==》wx.setClipboardData(Object object),如果有講的不清楚的地方,可以看官方文檔。鏈接地址:https://developers.weixin.qq.com/miniprogram/dev/api/device/clipboard/wx.setClipboardData.html
上圖是點(diǎn)擊復(fù)制的效果,很簡(jiǎn)單 ,只要拿過(guò)來(lái)api就能用,代碼如下:
// 點(diǎn)擊復(fù)制
copy:function(e){
let that=this;
wx.setClipboardData({
data: that.data.id, //這個(gè)是要復(fù)制的數(shù)據(jù)
success (res) {
wx.getClipboardData({
success (res) {
console.log(res.data) // data
}
})
}
})
},
上面實(shí)現(xiàn)的是點(diǎn)擊復(fù)制,用的是bindtap方法,如果需要實(shí)現(xiàn)長(zhǎng)按復(fù)制,將方法換成bindlongtap即可。