在Angular 7中,如果mat-input
在聚焦时不起作用,可以尝试以下解决方法:
MatInputModule
。在你的模块中,确保你添加了MatInputModule
到imports
数组中,如下所示:import { MatInputModule } from '@angular/material/input';
// ...
@NgModule({
imports: [
// ...
MatInputModule
],
// ...
})
export class YourModule { }
matInput
指令。在你的模板中,确保你在mat-input
元素上使用了matInput
指令,如下所示:
确保没有其他代码阻止了mat-input
的焦点。检查你的组件代码,确保没有其他代码阻止了mat-input
获得焦点,例如disabled
属性或其他事件处理程序。
如果以上方法都无法解决问题,可以尝试手动触发焦点。在你的组件中,你可以使用ViewChild
来获取到mat-input
元素的引用,并在需要时手动触发焦点,如下所示:
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { MatInput } from '@angular/material/input';
@Component({
// ...
})
export class YourComponent implements AfterViewInit {
@ViewChild(MatInput) inputElement: MatInput;
ngAfterViewInit() {
// 手动触发焦点
this.inputElement.focus();
}
}
这些解决方法应该能够帮助你解决在Angular 7中mat-input
在焦点上不起作用的问题。