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 网站上。

上一篇 下一篇
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 查看所有版本


暂无话题~