files
is defined to be FileList | null
so it can be null
.
You should either check for null
(using an if
) or use a "Non-null assertion operator" (!
) if you are sure it is not null
:
if(nativeElement.files != null) {
this.photoService.upload(this.vehicleId, nativeElement.files[0])
.subscribe(x => console.log(x));
}
//OR
this.photoService.upload(this.vehicleId, nativeElement.files![0])
.subscribe(x => console.log(x));
Note:
The "Non-null assertion operator" will not perform any runtime checks, it just tells the compiler you have special information and you know nativeElement.files
will not be null
at runtime.
If nativeElement.files
is null
at runtime, it will generate an error. This is not the safe navigation operator of other languages.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…