我正尝试在我的布局中使用它,我突然想到使用 Laravel 5.4 @slot 的新功能。
只是想知道是否可以将数组传递给插槽?
@section('SampleSection')
@component( 'mylayouts.partials.contentheader' )
@slot('title')
Sample Title
@endslot
@slot('indexes')
Pass array here // example [ 'a', 'b', 'c' ]
@endslot
@endcomponent
@endsection
最佳答案
据我所知,不可能通过@slot
将数组传递给组件。但是,可以将数组直接传递给组件,而无需使用插槽。
来自documentation :
Passing Additional Data To Components
Sometimes you may need to pass additional data to a component. For this reason, you can pass an array of data as the second argument to the @component directive. All of the data will be made available to the component template as variables:
@component('alert', ['foo' => 'bar']) ... @endcomponent
假设您有一组数据:
$my_array = ['a', 'b', 'c']
您可以像这样将它传递给组件:
@component('mylayouts.partials.contentheader', ['my_array' => $my_array])
...
@endcomponent
或者您可以直接传递它,而不使用变量:
@component('mylayouts.partials.contentheader', ['my_array' => ['a', 'b', 'c']])
...
@endcomponent
然后,在您的组件文件中,您将可以访问 $my_array
变量。例如,在您的 contentheader.blade.php
文件中,您可以这样做:
<ul>
@foreach($my_array as $value)
<li>{{ $value }}</li>
@endforeach
</ul>
关于php - 如何将数组传递给 laravel 5.4 中的@slot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909142/