【问题标题】:Laravel custom component parameter parsing issueLaravel 自定义组件参数解析问题
【发布时间】:2021-08-03 17:14:09
【问题描述】:

在我的 Laravel (7.x) 应用程序中。我正在尝试为链接创建一个组件:

组件:

<a class="links" href="{{ $route }}" title="{{ $title ?? null }}">
    @isset($icon)
        <i class="{{ $icon }}"></i>
    @endisset

    @isset($caption)
        <span>{{ $caption }}</span>
    @endisset
</a>
<x-link icon="{{ $icon }}" route="{{ route("admin.{$route}.create") }}" />
  OR
<x-link icon="{{ $icon }}" route="{{ route("admin." . $route. ".create") }}" />
  OR
<x-link icon="{{ $icon }}" route="{!! route("admin.{$route}.create") !!}" />
  OR
<x-link icon="{{ $icon }}" route="{!! route("admin." . $route. ".create") !!}" />

遇到这个问题:

syntax error, unexpected token "endif", expecting end of file (View: \path)

但是,如果我这样做,那么它会起作用:

$url = route("admin.{$route}.create");

...

<x-link icon="{{ $icon }}" route="{{ $url }}"></x-link>

我不喜欢声明变量仅供一次性使用,我喜欢直接传递值。因此,我更喜欢第一种方法。

为什么这个简单的代码不起作用..?

【问题讨论】:

    标签: php laravel components parameter-passing


    【解决方案1】:

    我发现如果值与参数绑定,参数效果会更好。

    <x-link icon="{{ $icon }}" :route="route('admin.' . $route . '.create')" />
    

    另外...如果需要多个值:

    <x-link icon="{{ $icon }}" :route="route('admin.' . $route . '.create')" :params="[
        'key1' => 'value1',
        'key2' => [
            ...
        ],
    ]" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2010-11-14
      相关资源
      最近更新 更多