我有一个binding an input using Svelte的简单演示应用程序。这是可行的,但是在相同元素上的单击处理程序中,我希望重置绑定值。
具体来说,我已将input
绑定到draftMessage
,并希望在提交邮件后将draftMessage
设置为空字符串:
<script>
let draftMessage = '';
let handleSendMessageClick = function(event){
var message = event.target.form.querySelector('input').value
previousMessages.push(message)
previousMessages = previousMessages
// Clear draftMessage
console.log(`Clearing draftMessage - why doesn't this work?`)
draftMessage = ''
}
let previousMessages = [];
</script>
<h1>Type some things</h1>
<form>
<input value={draftMessage}/>
<button on:click|preventDefault={handleSendMessageClick}>Send</button>
</form>
{#each previousMessages as message}
<p>{message}</p>
{/each}
如何在点击处理程序中更改绑定值?