小编mer*_*tik的帖子

ViewChild 返回“未定义” - Angular2

我试图通过按下父组件上的按钮来执行子组件的功能,但由于某种原因它未定义。

家长:

.. com1.html
  <ng-template #content let-c="close" let-d="dismiss">
    <div class="modal-header">
      <h4 class="modal-title">Title</h4>
    </div>
    <div class="modal-body" style="display: flex; justify-content: center;">
      <app-com2 [var1]="value" #traceRef></app-com2>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-outline-dark" (click)="savePNG()"><i class="fa fa-download"></i>PNG</button>
      <button type="button" class="btn btn-outline-dark" (click)="c()">Close</button>
    </div>
  </ng-template>

<button class="btn btn-sm btn-secondary" (click)="open(content)">Button</button>

...com1.ts
export class com1 implements OnInit {
@ViewChild('traceRef') traceRef;
value = "something";

  constructor(private modalService: NgbModal) {}

  savePNG() {
    this.traceRef.savePNG();
  }

  open(content: any) {
    this.modalService.open(content, { windowClass: 'temp-modal' });
  }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以指出我正确的方向,因为其他线程没有帮助:(即使使用 ngAfterViewInit 它仍然未定义。

viewchild angular

2
推荐指数
1
解决办法
3649
查看次数

标签 统计

angular ×1

viewchild ×1