angular - 类型 'copy' 上不存在属性 'Clipboard'

标签 angular clipboard

我正在尝试在单击按钮时复制文本。我尝试遵循 Angular 的文档 (https://material.angular.io/cdk/clipboard/overview#programmatically-copy-a-string),但我的终端在编译时给了我以下错误:

Property 'copy' does not exist on type 'Clipboard'

这是我的 .ts 文件:

export class myComponent implements OnInit {
  constructor(private clipboard: Clipboard) {}
  ngOnInit(): void {}

  copyTest() {
    this.clipboard.copy("test"); // Property 'copy' does not exist on type 'Clipboard'.
  }
}

这是我的 .html 文件:

<button [cdkCopyToClipboard]="copyTest()">Click to copy</button>

这是我的 app.module.ts 文件:

import { ClipboardModule } from "@angular/cdk/clipboard";

@NgModule({
  declarations: [
    AppComponent,
    myComponent,
  ],
  imports: [
    ClipboardModule,
  ]
})

任何想法为什么会发生此错误?

最佳答案

您一定错过了剪贴板的正确导入。

尝试将:import { Clipboard } from '@angular/cdk/clipboard'; 添加到您的 .ts 文件以及您的 app.module.ts。

还有一个界面剪贴板,您的 IDE 可能会接受它作为有效的 TS。

关于angular - 类型 'copy' 上不存在属性 'Clipboard',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69010650/

相关文章:

html - 从模式页面提交时启用 ionic 导航栏按钮

java - 双击将单词复制到剪贴板

javascript - 单行的Angular2多个ngFor

node.js - 如何在NeptuneDB中进行分页以实现高性能

c# - 在剪贴板中查找图像类型

keyboard-shortcuts - AwesomeWM urxvt 通过键盘粘贴剪贴板

python - 在 Windows 中使用 ctypes 从剪贴板读取文本

vba - excel宏cells.find由于某种原因不会使用变量

html - 在失去焦点之前等待点击

angular - 如何为 Angular 项目重用构建