Laravel Blade 模板:组件别名 1 个改进

介绍

如果组件存储在子目录中,你可能希望给它们起个别名以方便访问。

如何给组件别名

举例来说,如果一个 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
本文为 Wiki 文章,邀您参与纠错、纰漏和优化
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!