在 Laravel 6 中,Blade 模板中的 组件 & 插槽 示例
1、参考网址:https://learnku.com/docs/laravel/6.x/blade/5147#20f1dd 。Blade 模板 – 组件 & 插槽
2、先编写一个可复用的「alert」组件,我们想在应用中复用它,新建文件:/resources/views/alert.blade.php
<!-- /resources/views/alert.blade.php --> <div class="alert alert-danger"> <div class="alert-title">{{ $title }}</div> {{ $slot }} </div>
3、配置 /routes/web.php 中的路由
Route::get('component', function () { return view('component'); });
4、新建模板文件:/resources/views/component.blade.php
@component('alert') @slot('title') Forbidden @endslot You are not allowed to access this resource! @endcomponent
5、打开网址:view-source:http://laravel-theme-demo.local/component 。查看源代码。符合预期。如图1
<!-- /resources/views/alert.blade.php --> <div class="alert alert-danger"> <div class="alert-title">Forbidden</div> You are not allowed to access this resource! </div>
近期评论