danyderigon / livewire-bootstrap-modal
动态的 Laravel Livewire 3 Bootstrap 模态框。
dev-master
2023-12-16 06:44 UTC
Requires
- laravel/framework: ^8.0|^9.0|^10.0
- livewire/livewire: ^3.0
This package is not auto-updated.
Last update: 2024-09-22 02:21:50 UTC
README
本包允许您在 Bootstrap 模态框中动态显示 Laravel Livewire 3 组件。
警告:本包与 Livewire 2 不兼容。
文档
要求
- Bootstrap 5 必须先通过 webpack 安装
安装
需要此包
composer require danyderigon/livewire-bootstrap-modal
将 livewire:modals
组件添加到您的应用布局视图中
<livewire:modals/> <livewire:scripts/> <script src="{{ asset('js/app.js') }}"></script>
在您的应用 JavaScript 文件中需要 ../../vendor/danyderigon/livewire-bootstrap-modal/resources/js/modals
import('@popperjs/core'); import '../../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js'; import '../../vendor/danyderigon/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>
显示模态框
通过触发 showModal
事件并传递组件别名来显示模态框
<button type="button" wire:click="$dispatch('showModal', {alias: 'modals.create-post'})"> {{ __('Create post') }} </button>
挂载参数
在别名之后传递参数到组件的 mount
方法
<button type="button"wire:click="$dispatch('showModal', {alias: 'modals.update-post', params: {id: {{$post->id}} }})"> {{ __('Update Post #' . $post->id) }} </button>
上述示例中组件的 mount
方法可能如下所示
namespace App\Http\Livewire\Modals; use App\Models\Post; use Livewire\Component; class UpdatePost extends Component { public $post; public function mount(int $id) { $this->post = Post::find($id); } public function render() { return view('modals.update-post'); } }
隐藏模态框
通过触发 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
内的视图文件。包将使用此视图来渲染组件。