Laravel Blade 模板:组件别名
介绍
如果组件存储在子目录中,你可能希望给它们起个别名以方便访问。
如何给组件别名
举例来说,如果一个 Blade 组件存储在 resources/views/components/alert.blade.php
中,. 就可以使用 component
方法将 components.alert
的别名命名为 alert
。. 通常情况下,这一过程将在 AppServiceProvider
的 boot
方法中完成:
use Illuminate\Support\Facades\Blade;
Blade::component('components.alert', 'alert');
一旦组件有了别名,就可以使用一条指令渲染它:
@alert(['type' => 'danger'])
You are not allowed to access this resource!
@endalert
如果没有额外的插槽,还可以省略组件参数:
@alert
You are not allowed to access this resource!
@endalert