【问题标题】:Force a full page reload in Livewire在 Livewire 中强制重新加载整页
【发布时间】:2021-02-28 14:51:39
【问题描述】:

我在 Laravel 8 中使用 LivewireAlpine

我有一个带有 Datatable (jQuery) 和 Bootstrap 模式的页面。
该表中填充了模型实例列表中的一些数据。
当我单击表格中的按钮时,它会打开模式并允许编辑相应的记录。
这部分按预期工作。

然而,Datatable 库更难与 Livewire 一起使用,所以我决定在 <div> 中使用 wire:ignore 属性来限定整个 Datatable .
因此,如果我想在修改后刷新 Datatable,我不能使用魔法 $refresh,因为 Datatable 目前在 @ 内987654324@.

所以我想在编辑完成后重新加载整个页面,但我不知道该怎么做,return redirect()->back() 不起作用...

这就是我的save 方法的样子,当从模态框编辑完成时会调用它:

public function save()
{
    MyModel::where('id', $this->edited_id)->update([...]);
    $this->clearSelection();
    return redirect()->back(); // This is not working
}

这是我的桌子:

<div wire:ignore>
    <table class="dt-table table">
        <thead>
            <tr>
                <th>Actions</th>
                <th>id</th>
                <th>name</th>
                <th>other</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($models as $m)
                <tr>
                    <td>
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#mymodal">Edit</button>
                    </td>
                    <td>{{ $m->id }}</td>
                    <td>{{ $m->name }}</td>
                    <td>{{ $m->somedata }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
</div>

【问题讨论】:

  • 试试location.reload();
  • @KamleshPaul 您的意思是调用 Javascript 来初始化页面重新加载。我想知道 Livewire 组件(在 PHP 中)是否有直接的解决方案,比如 redirect() 或其他东西

标签: laravel laravel-livewire alpine.js


【解决方案1】:

Livewire 将原始 URL 存储在 Referer 标头中。您可以使用它来刷新页面:

return redirect(request()->header('Referer'));

【讨论】:

    【解决方案2】:

    检查您使用的是 Livewire 版本 2.x.x,并且您应该能够按照此处的文档进行操作:

    https://laravel-livewire.com/docs/2.x/redirecting

    Livewire 组件:

    class ContactForm extends Component
    {
        public $email;
    
        public function addContact()
        {
            Contact::create(['email' => $this->email]);
    
            return redirect()->to('/contact-form-success');
        }
    }
    

    Livewire 组件模板

    <div>
        Email: <input wire:model="email">
    
        <button wire:click="addContact">Submit</button>
    </div>
    

    【讨论】:

    • 所以我应该重定向到它自己?
    • 是的。我通常只是重定向到同一页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    相关资源
    最近更新 更多