[英]Typescript: Object is possibly null when creating method
I am getting an "Object is possibly null" error when creating the changePageSize method inside book-store.component.html.在 book-store.component.html 中创建 changePageSize 方法时,出现“对象可能为空”错误。 Apparently I have to initialize the object in the class, but I'm not sure how to do that, can anyone help me out.
显然我必须初始化类中的对象,但我不知道如何做到这一点,任何人都可以帮助我。 I am new to Typescript and Angular.
我是 Typescript 和 Angular 的新手。
$event.target
has type EventTarget
. $event.target
类型为EventTarget
。 It has no value
field.它没有
value
字段。 That's the reason of your issue.这就是你的问题的原因。
To fix this issue you need to cast $event.target
to correct type.要解决此问题,您需要将
$event.target
为正确的类型。
// template
<select ... (change)="changePageSize($event)">...</select>
// component
changePageSize(event: Event): void {
const element = event.target as HTMLInputElement;
const value = element.value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.