Laravel 8 插槽

2021-07-03 14:48 更新

通常,你需要通過 「slots」 向組件傳遞附加內(nèi)容。 假設(shè)我們創(chuàng)建的 alert 組件具有以下標(biāo)記:

<!-- /resources/views/components/alert.blade.php -->

<div class="alert alert-danger">
    {{ $slot }}
</div> 

我門可以通過向組件注入內(nèi)容的方式,將內(nèi)容傳遞到 slot :

<x-alert>
    <strong>Whoops!</strong> Something went wrong!
</x-alert> 

有時候一個組件可能需要在它內(nèi)部的不同位置放置多個不同的插槽。我們來修改一下 alert 組件,使其允許注入 「title」:

<!-- /resources/views/components/alert.blade.php -->

<span class="alert-title">{{ $title }}</span>

<div class="alert alert-danger">
    {{ $slot }}
</div>

您可以使用 x-slot 標(biāo)簽來定義命名插槽的內(nèi)容。任何沒有在 x-slot 標(biāo)簽中的內(nèi)容都將傳遞給 $slot 變量中的組件:

<x-alert>
    <x-slot name="title">
        Server Error
    </x-slot>

    <strong>Whoops!</strong> Something went wrong!
</x-alert>
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號