admin管理员组文章数量:1415467
On Svelte 3.12.1, when event handler onFilesChange is triggered, it unexpectedly triggers onFileClick.
<script>
let files = [];
function previewImage(file) {
return "<img src='" + file.previewURL + "' />";
}
function onFileClick(file) {
console.log("onFileClick");
files.splice(files.findIndex(x => x.name === file.name), 1);
files = files;
}
function onFilesChange(event) {
console.log("onFilesChange");
let inputFiles = event.target.files;
for (var i = 0; i < inputFiles.length; i++)
files.push({inputFile: inputFiles.item(i), previewURL: URL.createObjectURL(inputFiles.item(i))});
files = files;
}
</script>
<input accept="image/*" multiple name="files" type="file" on:change={onFilesChange}/>
{#each files as file}
<figure on:click={onFileClick(file)}>
{@html previewImage(file)}
</figure>
{/each}
But if I slightly change to using arrow functions, then it works as expected. Why is this happening, and what is the proper way to handle DOM events in Svelte 3?
<input accept="image/*" multiple name="files" type="file" on:change={(event) => onFilesChange(event)}/>
{#each files as file}
<figure on:click={(file) => onFileClick(file)}>
{@html previewImage(file)}
</figure>
{/each}
On Svelte 3.12.1, when event handler onFilesChange is triggered, it unexpectedly triggers onFileClick.
<script>
let files = [];
function previewImage(file) {
return "<img src='" + file.previewURL + "' />";
}
function onFileClick(file) {
console.log("onFileClick");
files.splice(files.findIndex(x => x.name === file.name), 1);
files = files;
}
function onFilesChange(event) {
console.log("onFilesChange");
let inputFiles = event.target.files;
for (var i = 0; i < inputFiles.length; i++)
files.push({inputFile: inputFiles.item(i), previewURL: URL.createObjectURL(inputFiles.item(i))});
files = files;
}
</script>
<input accept="image/*" multiple name="files" type="file" on:change={onFilesChange}/>
{#each files as file}
<figure on:click={onFileClick(file)}>
{@html previewImage(file)}
</figure>
{/each}
But if I slightly change to using arrow functions, then it works as expected. Why is this happening, and what is the proper way to handle DOM events in Svelte 3?
<input accept="image/*" multiple name="files" type="file" on:change={(event) => onFilesChange(event)}/>
{#each files as file}
<figure on:click={(file) => onFileClick(file)}>
{@html previewImage(file)}
</figure>
{/each}
Share
Improve this question
edited Sep 22, 2019 at 20:11
JuniorSD
asked Sep 22, 2019 at 20:02
JuniorSDJuniorSD
491 silver badge5 bronze badges
1 Answer
Reset to default 6The on:whatever
event handler values need to be functions that will be called with the DOM event as an argument. on:change={onFilesChange}
is fine because onFilesChange
is a function which expects an event as its argument. on:click={onFileClick(file)}
is not because that will call onFileClick(file)
right away, and the return value from that is expected to be an event handler.
You should use on:click={() => onFileClick(file)}
so that the event handler is a function, which will be passed the DOM event (which gets discarded) and then calls onFileClick(file)
.
本文标签: javascriptSvelte 3 Event Handling not Working as ExpectedStack Overflow
版权声明:本文标题:javascript - Svelte 3: Event Handling not Working as Expected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745182341a2646522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论