我一直在寻找一种方法来创建一个组件,但根据需要创建任意多的插槽。
例如,你想在你的项目中多次重复一个菜单列表的风格,最好的选择是有一个组件,但如果我需要自定义html选项的每个元素,这使得它棘手。
到目前为止,我得到的最好的选项是以下,希望有人发现它的帮助。
在您的视图文件中。
@component('components.radioList',['values'=>[1,2,3,4],'name'=>'myname'])
@slot('html_0')
mi html item 0
@endslot
@slot('html_1')
mi html item 1
@endslot
@slot('html_2')
mi html item 2
@endslot
@slot('html_3')
mi html item 3
@endslot
@endcomponent
在你的组件刀片中
<ul class="list-group">
@foreach($values as $value)
@isset(${"html_{$loop->index}"})
<li class="list-group-item custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck_{{$loop->index}}" name="{{$name}}" value="{{$value}}">
<label class="custom-control-label" for="customCheck_{{$loop->index}}">
{{ ${"html_{$loop->index}"} }}
</label>
</li>
@endisset
@endforeach
</ul>
唯一的问题是,这些值和插槽的数量必须匹配。
如果您不需要为每个槽创建额外的值,您可以将其创建为一个计数。
@component('components.radioList',['count'=>4,'name'=>'myname'])
并将Foreach改为For
干杯!