Flash Messages

未匹配的标注
本文档最新版为 2.x,旧版本可能放弃维护,推荐阅读最新版!

闪存消息

如果有用的是向用户“刷新”成功或失败消息,Livewire 支持 Laravel 的系统将数据刷新到会话.

这是此用法的一个常见示例:

use Livewire\Component;

class UpdatePost extends Component
{
    public $post;
    public $title;

    public function mount(Post $post)
    {
        $this->post = $post;
        $this->title = $post->title;
    }

    public function update()
    {
        $this->post->update([
            'title' => $this->title,
        ]);

        session()->flash('message', 'Post successfully updated.');
    }

    public function render()
    {
        return view('livewire.update-post');
    }
}
<form wire:submit.prevent="update">
    <div>
        @if (session()->has('message'))
            <div class="alert alert-success">
                {{ session('message') }}
            </div>
        @endif
    </div>

    Title: <input wire:model="title" type="text">

    <button>Save</button>
</form>

现在,在用户单击“保存”并更新其帖子后,他们将在页面上看到“帖子已成功更新”的提示。

如果您希望将 Flash 数据添加到重定向中,并在目标页面上显示该消息, Livewire 是足够聪明的,可以将 Flash 数据持久化以用于另一个请求。 例如:

public function update()
{
    $this->post->update([
        'title' => $this->title,
    ]);

    session()->flash('message', 'Post successfully updated.');

    return redirect()->to('/posts');
}

现在,当用户“保存”帖子时,他们将被重定向到 /posts 端点,并在那里看到刷新消息。 假定 /posts 页面具有正确的 Blade 片段以显示 Flash消息。

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~