Witam wszystkich.
Chciałbym zasięgnąć porady.
Mam proste zdarzenie:
W widoku HTML (Angular):
<input type="file" class="file-input" (change)="onImageUpload($event)">
Następnie w Component:
imageDisplay;
onImageUpload(event) {
const file: File = event.target.files[0];
if (file) {
const fileReader = new FileReader();
fileReader.onload = () => {
this.imageDisplay = fileReader.result;
};
fileReader.readAsDataURL(file);
}
}
W trybie strict mode: false oczywiście wszystko działa. Natomiast w strict mode niestety już nie.
Błąd: Parameter 'event' implicitly has an 'any' type.
Robię tak:
onImageUpload(event: Event) {
const file: File = event.target.files[0];
if (file) {
const fileReader = new FileReader();
fileReader.onload = () => {
this.imageDisplay = fileReader.result;
};
fileReader.readAsDataURL(file);
}
}
ale od razu dostaję komunikat:
'event.target' is possibly 'null'.
57 const file: File = event.target.files[0];
oraz:
Property 'files' does not exist on type 'EventTarget'.
57 const file: File = event.target.files[0];
Próbowałem również tak:
onImageUpload(event: Event) {
if (event.target) {
const file: File = event.target.files[0];
if (file) {
const fileReader = new FileReader();
fileReader.onload = () => {
this.imageDisplay = fileReader.result;
};
fileReader.readAsDataURL(file);
}
}
}
Bardzo proszę o wyjaśnienie jak to napisać w strict mode.