html - 无法读取未定义的属性 'nativeElement' - ngAfterViewInit

标签 html angular typescript angular2-forms angular2-directives

我正在尝试使用 this example 添加“剪贴板”指令.该示例现在已过时,因此我不得不更新它获取 nativeElement 的方式。

我遇到了错误

Cannot read property 'nativeElement' of undefined

我在代码中用 <===== error here 标记了错误:

clipboard.directive.js

import {Directive,ElementRef,Input,Output,EventEmitter, ViewChild, AfterViewInit} from "@angular/core";
import Clipboard from "clipboard";

@Directive({
  selector: "[clipboard]"
})
export class ClipboardDirective implements AfterViewInit {
  clipboard: Clipboard;

   @Input("clipboard")
   elt:ElementRef;

  @ViewChild("bar") el;

  @Output()
  clipboardSuccess:EventEmitter<any> = new EventEmitter();

  @Output()
  clipboardError:EventEmitter<any> = new EventEmitter();

  constructor(private eltRef:ElementRef) {
  }

  ngAfterViewInit() {
    this.clipboard = new Clipboard(this.el.nativeElement, {   <======error here
      target: () => {
        return this.elt;
      }
    } as any);

    this.clipboard.on("success", (e) => {
      this.clipboardSuccess.emit();
    });

    this.clipboard.on("error", (e) => {
      this.clipboardError.emit();
    });
  }

  ngOnDestroy() {
    if (this.clipboard) {
      this.clipboard.destroy();
    }
  }
}

html

<div  class="website" *ngIf="xxx.website !== undefined"><a #foo href="{{formatUrl(xxx.website)}}" target="_blank" (click)="someclickmethod()">{{xxx.website}}</a></div>
                                        <button #bar [clipboard]="foo" (clipboardSuccess)="onSuccess()">Copy</button>

如何消除该错误?

已更新为不使用 AfterViewInit,因为它不是 View ...同样的错误:

@Directive({
  selector: "[clipboard]"
})
export class ClipboardDirective implements OnInit {
  clipboard: Clipboard;

   @Input("clipboard")
   elt:ElementRef;

  @ViewChild("bar") el;

  @Output()
  clipboardSuccess:EventEmitter<any> = new EventEmitter();

  @Output()
  clipboardError:EventEmitter<any> = new EventEmitter();

  constructor(private eltRef:ElementRef) {
  }

  ngOnInit() {
    this.clipboard = new Clipboard(this.el.nativeElement, {
      target: () => {
        return this.elt;
      }
    } as any);

我想我不需要使用@viewChild,因为它不是一个组件,但我不确定如何填充eleltRefel 仅用于替换 eltRef,因为我无法填充 eltRef

最佳答案

您将 ElementRef 命名为 eltRef,但尝试在 ngAfterViewInit 中使用 this.el。您需要使用相同的名称。

这会起作用:

constructor(private el:ElementRef) {
}

ngAfterViewInit() {
  this.clipboard = new Clipboard(this.el.nativeElement, { 
  target: () => {
    return this.elt;
  }
} 

关于html - 无法读取未定义的属性 'nativeElement' - ngAfterViewInit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43712642/

相关文章:

javascript - 动态添加颜色到添加的文本

javascript - gapi 有时在 Angular 6 中未定义

angular - 我如何为 Angular 2 中的开发(而非测试)提供模拟服务?

angular - 类型 '{Property : string, Property2: string} 不可分配给类型 Observable<Filters[]>

javascript - Cognito 从预注册 Lambda 触发器发送什么样的事件

javascript - 在 Javascript 中访问表格单元格的内容

javascript - 如何通过 javascript 更改 html5 视频的 src 而不会崩溃 chrome 或泄漏内存?

typescript - 如何在 typescript 中输入 "cross-product"类型?

reactjs - Intersection Observer API 进入无限渲染循环

html - 记录 Chrome 中 DOM 结构的变化