mozhuilungdsuo/livewire-bootstrap-modal

动态Laravel Livewire 3 Bootstrap模态框。

v1.0.4 2024-05-30 06:04 UTC

This package is auto-updated.

Last update: 2024-08-30 06:46:40 UTC


README

此包允许您在Bootstrap模态框中动态显示您的Laravel Livewire 3组件。

警告:此包与Livewire 2不向后兼容。

文档

要求

  • Bootstrap 5必须先通过webpack安装

安装

需要此包

composer require mozhuilungdsuo/livewire-bootstrap-modal

livewire:modals组件添加到您的应用布局视图中

<livewire:modals/>
<livewire:scripts/>
<script src="{{ asset('js/app.js') }}"></script>

在您的应用JavaScript文件中引入../../vendor/mozhuilungdsuo/livewire-bootstrap-modal/resources/js/modals

import('@popperjs/core');
import '../../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js';
import '../../vendor/mozhuilungdsuo/livewire-bootstrap-modal/resources/js/modals.js';

用法

模态视图

创建一个您想显示为模态框的Livewire组件。该组件的视图必须使用Bootstrap modal-dialog 容器

<div>

        <div class="modal-header">
            <h5 class="modal-title">Modal title</h5>
            <button type="button" class="btn-close" wire:click="$dispatch('hideModal')" aria-label="Close"></button>
        </div>
        <div class="modal-body">
            <p>Modal body text goes here.</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary"  wire:click="$dispatch('hideModal')">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
</div>
 

从普通blade视图调用

<button type="button" onclick="Livewire.dispatch('showModal', {data: {'alias' : 'modals.example'}})">
                            Click me to open modal
                        </button>

显示模态框

通过触发带有组件别名的 showModal 事件来显示模态框

<div>
    <button type="button"wire:click="$dispatch('showModal', {data: {'alias' : 'modals.example'}})">
Click me
    </button>
</div>

挂载参数

在别名之后传递参数到组件的 mount 方法

<div>
    <button type="button"wire:click="$dispatch('showModal', {data: {'alias' : 'modals.example','params' :{'name':'test'},'backdrop':'static',message:'Are you sure?'}})">
Click me
    </button>
</div>
- Note 'backdrop':'static',message:'Are you sure?' are optional 

上面示例中的组件 mount 方法看起来像这样

namespace App\Http\Livewire\Users;

use App\Models\User;
use Livewire\Component;

class Update extends Component
{
    public $user;
    
    public function mount(User $user)
    {
        $this->user = $user;
    }
    
    public function render()
    {
        return view('users.update');
    }
}

隐藏模态框

通过触发 hideModal 事件来隐藏当前打开的模态框

<button type="button" wire:click="$dispatch('hideModal')">
    {{ __('Close') }}
</button>

触发事件

您可以在视图中触发事件

<button type="button" wire:click="$dispatch('hideModal')">
    {{ __('Close') }}
</button>

或在组件内部,就像任何正常的Livewire事件一样

public function save()
{
    $this->validate();

    // save the record

    $this->dispatch('hideModal');
}

发布资产

自定义视图

通过发布包视图使用您自己的模态框视图

php artisan vendor:publish --tag=livewire-bootstrap-modal:views

现在编辑位于 resources/views/vendor/livewire-bootstrap-modal 内的视图文件。包将使用此视图来渲染组件。