如何将带有刀片的路由添加到组件的参数中

我想这很简单,但我花了 30 分钟试图弄清楚如何正确编写:


@component('components.button', [

    'color' => 'danger',

    'formaction' => 'formaction="'{{ route('profiles.destroy', $user->id) }}'"'

    ])

@lang('Supprimer')

我的按钮组件:


<button type="submit" class="btn @isset($color){{ ' btn-' . $color }}@else btn-primary @endisset

float-right " @isset($formaction){{ $formaction }}>

{{ $slot }}


小唯快跑啊
浏览 123回答 2
2回答

一只斗牛犬

以@包含 PHP 代码开头的刀片指令,这意味着您的字符串连接和刀片变量替换不起作用。它实际上更简单:@component('components.button', [&nbsp; &nbsp; 'color' => 'danger',&nbsp; &nbsp; 'formaction' => route('profiles.update', $user->id)])@endcomponent@lang('Supprimer')<button type="submit"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; class="btn @isset($color){{ ' btn-' . $color }}@else btn-primary @endissetfloat-right "&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @isset($formaction)formaction="{{ $formaction }}"@endisset>&nbsp; &nbsp;{{ $slot }}</button>之前执行计算时,您可以使组件代码更好一点:@php($btnColorClass = isset($color) ? 'btn-'.$color : 'btn-primary')<button type="submit"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; class="btn {{ $btnColorClass }} float-right"&nbsp; &nbsp; &nbsp; &nbsp; @isset($formaction)formaction="{{ $formaction }}"@endisset>&nbsp; &nbsp;{{ $slot }}</button>

一只名叫tom的猫

在按钮上试试。<button type="submit" class="btn @if(isset($color)) {{ ' btn-'.$color }} @else {{ 'btn-primary' }} @endif float-right "@if(isset($formaction)) action="{{ $formaction }}" @endif>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ $slot }}&nbsp; &nbsp; </button>
打开App,查看更多内容
随时随地看视频慕课网APP