我在构建 Angular 应用程序时遇到错误。
HTML 模板
<mat-form-field *ngIf="requested">
<input
matInput
(input)="sendingAccesscode($event)"
placeholder="{{ 'CODE_LABEL' | i18next }}"
name="accesscode"
formControlName="accesscode"
minlength="6"
required
/>
</mat-form-field>
typescript
sendingAccesscode(event: { target: { value: string }}) {
if (event.target?.value.length >= 6) {
this.accessCode.emit(event.target.value);
}
}
我收到以下错误:
error TS2345:
Argument of type 'Event' is not assignable to parameter of type '{ target: { value: string; }; }'.
Types of property 'target' are incompatible.
Type 'EventTarget | null' is not assignable to type '{ value: string; }'.
Type 'null' is not assignable to type '{ value: string; }'.
13 (input)="sendingAccesscode($event)"
~~~~~~```
最佳答案
如何将其设置为 Event
类型,这将解决您的问题!
您还可以使用InputEvent
sendingAccesscode(event: Event) {
const value = (event.target as HTMLInputElement).value;
if (value && value.length >= 6) {
console.log(value);
}
}
关于javascript - 错误 TS2345 : Argument of type 'Event' is not assignable to parameter of type '{ target: { value: string; }; }' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73329921/