php - 从父级中的当前循环迭代访问组件数据 - Laravel 组件 Scoped Slot
问题描述
我有一个组件表
<x-table :rows="$rowsArray"></x-table>
我传递数组数组,其中每个内部数组代表<tr>
要打印的行。
$rowsArray = [
['name'=>'Marko', 'age'=>21],
['name'=>'John', 'age'=>33]
]
在组件table.blade.php内部:我将每个值打印为简单文本
<table>
@foreach($rowsArray as $tr)
<tr>
@foreach($tr as $td_key => $td_value)
<td>
{{ $td_value }}
</td>
@endforeach
</tr>
@endforeach
</table>
由于它只是打印字符串文本,因此对于我希望在内部打印不同布局的情况,我<td>
通过插槽$customSlot传递布局。
通过插槽调用具有自定义布局的组件
硬编码示例 ->
<x-table :rows="$rowsArray">
<x-slot name="customSlot">
<div class="flex">
<span>
Marko
</span>
<span>
21 age
</span>
</div>
</x-slot>
</x-table>
所以更新table.blade.php中的代码 我正在检查插槽是否通过,如果没有打印字符串文本。
<table>
@foreach($rowsArray as $tr)
<tr>
@foreach($tr as $td_key => $td_value)
<td>
@if(isset($customSlot))
{{ $customSlot }}
@else
{{ $td_value }}
@endif
</td>
@endforeach
</tr>
@endforeach
</table>
问题:
我想在插槽范围内访问$tr
当前foreach
迭代的数据,而不是打印硬编码值。
这里$tr['name']
和$tr['age']
是未定义的,我希望它可以从当前迭代中访问:
<x-slot name="customSlot">
<div class="flex">
<span>
{{ $tr['name'] }}
</span>
<span>
{{ $tr['age'] }} age
</span>
</div>
</x-slot>
投掷
$tr is undefined
传递数组的预期输出:
<table>
<tr>
<td>
<div class="flex">
<span>Marko</span>
<span>21 age</span>
</div>
</td>
<td>
<div class="flex">
<span>John</span>
<span>33 age</span>
</div>
</td>
</tr>
</table>
解决方案
推荐阅读
- file - 创建文本文件以写入/添加几个图像名称
- class - 对象中的变量范围
- android - 当授予权限时,Android 10 上的 ACTIVITY_MANAGE_OVERLAY_PERMISSION 令人困惑,但仍然抛出异常
- amazon-web-services - 用于普罗米修斯的弹性搜索导出器抛出 403 错误
- xbrl - 是否有一种程序化的方式来确定 SEC 备案的最新背景是什么?
- neo4j - Grandstack 和 Forcegraph
- java - TestNG - 如何将组存储和检索为对象
- python - 如何在pygame中删除绘制的矩形?
- html - 仅在移动设备上制作 iframe 全屏的最佳方法是什么?
- linux - inotify 似乎在其输出中的文件名中添加了一个 6 字母代码,在扩展名之前