首页 > 解决方案 > navigator.clipboard.readText() 的等效代码是什么?

问题描述

navigator.clipboard.readText()
  .then(text => {
    // `text` contains the text read from the clipboard
  })
  .catch(err => {
    // maybe user didn't grant access to read from clipboard
    console.log('Something went wrong', err);
  });

上面的代码来自

我需要从剪贴板读取按钮单击角度
如何做到这一点?

标签: angularclipboarddataclipboard-interaction

解决方案


通过您的问题,我看到您遗漏了一些重要的东西。您的 Angular 应用程序是用typescript编写的,即:

JavaScript 的类型化超集,可编译为纯 JavaScript

这意味着您提到的剪贴板 API可以在 Angular 中使用!正如您在这个使用您的代码的演示中看到的那样。

当前 typescript 的navigator类型为DefinedTyped,但不幸的是它仍然不包含剪贴板 API,因为它仍然是非标准 DOM API,正如您在这个 git 已关闭问题中所见 - Missing 'Navigator.clipboard' (clipboard asynchronous API)


推荐阅读