繁体   English   中英

Laravel Livewire wire:submit.prevent 没有按预期工作

[英]Laravel Livewire wire:submit.prevent not working as intended

我有一个简单的表格:

<form class="w-full" wire:submit.prevent="postComment">
     <textarea 
         type="text" 
         name="comment" 
         id="comment"
         wire:model.defer="newCommentState.body" 
         class="border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm mt-1 block w-full" 
         placeholder="Leave a comment!">
    </textarea>
    <button 
        type="submit" 
        class="justify-items-start btn rounded-full m-3">
        Comment
    </button>
</form>
@error('newCommentState.body')
    <p class="mt-2 text-sm text-red-500">{{ $message }}</p>
@enderror

和 Livewire 组件:

public $newCommentState = [
    'body' => ''
];

public function postComment()
{
   dd($this->newCommentState);
}

以上所有代码都在 Livewire 2x 和 alpine 3x 上运行,@livewire 脚本包含在 \layout\app.blade.php 中。

但出于某种原因,当我单击提交按钮时,它会重新加载页面并将“?comment=”附加到 url。

我认为问题是兑现的观点,所以我运行了php artisan view:clearphp artisan optimize:clear ,但这并没有帮助。 我还清除了我的浏览器缓存,认为这会有所帮助,但没有成功。

有人可以启发这个问题的解决方案,或者指出我可能做错了什么。

Thanks In Advance

按照此页面https://laravel-livewire.com/docs/2.x/troubleshooting ,应该有根元素问题

Livewire 要求在组件刀片视图的根部只有一个 HTML 元素,这不是你的情况,你有一个 Form 和一个 P。

解决方法是确保你只有一个根 HTML 元素,比如 a. 如果您有多个元素,请将所有内容包装在一个或另一个适合您布局的元素中。

你的代码应该是这样的:

 <div> <form class="w-full" wire:submit.prevent="postComment"> <textarea type="text" name="comment" id="comment" wire:model.defer="newCommentState.body" class="border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm mt-1 block w-full" placeholder="Leave a comment."> </textarea> <button type="submit" class="justify-items-start btn rounded-full m-3"> Comment </button> </form> @error('newCommentState.body') <p class="mt-2 text-sm text-red-500">{{ $message }}</p> @enderror </div>

希望这有帮助!

也许你忘了在关闭 body 标签之前把@livewireScripts放在最后

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM