在我的Vue项目中,我有一个复制按钮,该按钮应该在单击时从数据中复制属性值:
<span class="btn-action" :class="!data.value.copy ? 'btn-disabled' : 'btn-active'" @click="copyCode(data.item)"><font-awesome-icon :icon="['far','copy']" /></span>
和我的copyCode函数:
copyCode: function(item) {
// tests if item exists...
let copyText = item.code;
if (navigator && navigator.clipboard) {
console.log('navigator:',navigator);
// this works on chrome/firefox
navigator.clipboard.writeText(copyText);
} else {
console.log('no navigator');
this.writeTextIOS(copyText);
}
}
writeTextIOS: function(str) {
return new Promise(function(resolve,reject) {
let success = false;
function listener(e) {
e.clipboardData.setData("text/plain",str);
e.preventDefault();
success = true;
}
document.addEventListener("copy",listener);
document.execCommand("copy");
document.removeEventListener("copy",listener);
success ? resolve() : reject();
});
},
我在safari上发现了导航器,但是它没有剪贴板属性(我可能也可以检查navigator.userAgent
,但这并不是确定的地方,因为它在进入时肯定会进入else语句我正在使用safari)。
我从这里获得了writeTextIOS函数:https://gist.github.com/lgarron/d1dee380f4ed9d825ca7但我仅得到一个Unhandled Promise Rejectio: undefined
异常。
那我该怎么办。我不想使用任何第三方程序包,例如剪贴板(现在)。有没有针对我的问题的“本地”解决方案?