Livewire关闭时清除模态

当您有一个已部分填充的模型并使用 Alpine 关闭模态时,即:

<button type="button" @click="on = false">Close</button>

模态将关闭,但任何输入到模态内表单的内容都将保留。

要在模型关闭时清除表单,请改为调用组件上的方法:

<button type="button" wire:click="close">Close</button>

接下来,在类中创建方法并调用 $this->reset() 来重置类上的所有属性或指定要清除的属性。

最后触发一个名为 close-modal 的事件

public function close()
{
    $this->reset(['name', 'email']);
    $this->dispatchBrowserEvent('close-modal');
}

 

发表回复

您的电子邮箱地址不会被公开。