页面加载输入应集中
<mat-form-field>
<input matInput #element placeholder="username" >
<button type="button" (click)="focus()" class="btn btnStyles" >HighLight</button>
@ViewChild('element') ref:ElementRef;
focus(){
this.ref.nativeElement.focus();
}
单击 HighLight 按钮时,它会聚焦文本框,但是当我在 ngOnInit 上调用此方法时,它会给出错误无法读取未定义的属性“nativeElement”
ngOnInit() {
this.focus();
}