useCopy.tsx 501 B

1234567891011121314151617
  1. import { message } from 'antd'
  2. function useCopy() {
  3. const copy = (str: string) => {
  4. let element = document.createElement("textarea");
  5. element.id = 'myTextarea'
  6. element.textContent = str
  7. document.body.append(element);
  8. (document.getElementById('myTextarea') as any).select();
  9. document.execCommand("Copy")
  10. document.body.removeChild(element);
  11. message.success(`复制成功:${str}`)
  12. }
  13. return { copy }
  14. }
  15. export default useCopy