0

I tried to implement a copy function for an ID using useClipBoard, but after writing the code, it didn't copy the ID as expected. my code like this, i cant find why it doesnt work.

<ImIconButton
  name="icon-outlined-copy"
  @click="() => copyText(`${templateId}`)"
></ImIconButton>
const copyAbility = async (text: string) => {
  if (!text) {
    return proxy.$message({
      type: 'error',
      message: $t('op.appManageBackstage.basicInfo.alertToast.copyFailed'),
    });
  }
  try {
    await copy(text);
    proxy.$message({
      type: 'success',
      message: $t('op.appManageBackstage.basicInfo.alertToast.copySuccess'),
    });
  } catch (e) {
    proxy.$message({
      type: 'error',
      message: $t('op.cardTool.cardDisplay.alertToast.modificationFailed'),
    });
  }
};
const copyText = throttleByThreeSeconds(copyAbility);

I expect that clicking on this button will copy the ID.

zoe
  • 1
  • The code shown is not very helpful. You're calling functions without showing what those functions are. What is `await copy(text)` doing? This function I assume is relevant to your problem but you need to show the code for that function, which is also where I assume `useClipBoard` is being used. It's also not clear why we should know what `useClipBoard` is or means. If this is a third party library or function, make sure to reference where you got it from. – yoduh Jul 10 '23 at 14:42
  • Lastly, you should show where the param `templateId` comes from, how it's defined and how it's used, including some example values you've at least verified are being sent. For all we know, this param has no value so there is no reason why any text from it should be copied. Please review [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and edit your question to provide **all** relevant code. – yoduh Jul 10 '23 at 14:47
  • Thank you for your enthusiastic answer. the param is ok. I think I may have found the reason. https://stackoverflow.com/questions/51805395/navigator-clipboard-is-undefined Thank you once again for your answer. It's awesome. – zoe Jul 11 '23 at 07:28

0 Answers0