大家,目前我正在做一些 Laravel 專案,我希望有一個 x-blade 組件,它根據傳遞給組件的屬性來呈現 html,但我不希望屬性本身被呈現到 html 中。
組件刀片檔案:
@props([
'variant'=>'red',
'styles'=>[
'red'=>'bg-red text-white h-11 px-4 rounded-xl flex justify-center items-center font-bold',
'white'=>'bg-white border border-red h-11 px-4 rounded-xl flex justify-center items-center font-bold'
]
])
<a href="{{$attributes['href']}}" {{$attributes->merge(['class'=>$styles[$variant]])}}>
{{$slot}}
@if($attributes['external'])
<div class="w-4 xl:w-5 4xl:w-6 ml-3 sm:ml-4">
I'm external
</div>
@endif
</a>
我的組件:
<x-test href="#" class="w-max" variant="white" external>
Button text
</x-test>
呈現的html:
<a href="#" class="bg-white border border-red h-11 sm:h-12 xl:h-13 4xl:h-14 px-4 sm:px-7 xl:px-8 4xl:px-9 rounded-xl flex justify-center items-center font-bold w-max" external="external">
Button text
<div class="w-4 xl:w-5 4xl:w-6 ml-3 sm:ml-4">
I'm external
</div>
</a>
如您所見,<a>標簽正在使用external="external"屬性呈現,我想阻止這種情況。
非常感謝您的時間和精力!!!:)
uj5u.com熱心網友回復:
只需$external在道具中宣告以防止這種情況并像普通變數一樣使用它
@props([
...
'external'=false
])
<a {{ $attributes }} {{$attributes->merge(['class'=>$styles[$variant]])}}>
{{$slot}}
@if($external)
<div class="w-4 xl:w-5 4xl:w-6 ml-3 sm:ml-4">
I'm external
</div>
@endif
</a>
uj5u.com熱心網友回復:
或者試試這個:
$attributes->merge(['class'=>$styles[$variant]])->except('external')
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/421724.html
標籤:
上一篇:如何僅在Laravel中驗證電子郵件時檢查電子郵件是否存在?
下一篇:查詢無法運行的簡單Laravel
