问题
类型错误:无法读取未定义的属性“nativeElement”
组件 1
<div id="formkeyRefId" #formkeyRefId (click)="test.reloadContent(data.value)> </div>
<ng-container>
<app-component2 #test [data]="data.value" ></app-component2>
</ng-container>
</div>
export class Component1 implements OnInit, AfterViewInit {
@ViewChild('formkeyRefId', { static: false }) formkeyRefId: ElementRef;
constructor() { }
ngOnInit() { }
ngAfterViewInit() {
console.log(this.formkeyRefId.nativeElement);
this.formkeyRefId.nativeElement.click();
setTimeout(() => {
console.log(this.formkeyRefId.nativeElement.id);
this.formkeyRefId.nativeElement.click();
}, 5000);
}
组件 2 app-component2
import { Component, Input, OnInit, Output, EventEmitter ,ElementRef, ViewChild,AfterViewInit } from '@angular/core';
export class Component2 implements OnInit,AfterContentInit {
@ViewChild('formkeyRefId',{static: false}) formkeyRefId: ElementRef;
constructor(public myElement: ElementRef){}
this.afterViewInitExecuted = false;
ngAfterContentInit() {
this.afterViewInitExecuted = true;
}
formEventEmitter(event) {
if (event && this.afterViewInitExecuted) {
setTimeout(() => {
console.log(this.formkeyRefId.nativeElement);
}, 5000);
//this.formkeyRefId.nativeElement.click();
//const el: HTMLElement = this.myDiv.nativeElement;
//e1.click();
}
reloadContent() {
getdata();
}
}
试图测试 id 是否存在于另一个组件中
ngAfterViewInit() {
this.afterViewInitExecuted = true;
console.log(this.formkeyRefId); Output Undefined
}
MM们
慕雪6442864
慕桂英3389331
相关分类