3

在 Laravel 7 中,我开始使用View Components。我正在尝试将$attributes变量从一个组件传递到另一个组件,例如:

x-模态组件:

<div {{ $attributes->merge(['class' => 'modal fade']) }}>
   Something great...
</div>

x-modal-form 组件:

<x-modal {{ $attributes }}>
    Something great too
</x-modal>

在这种情况下,我在 x-modal 组件中有一个id属性,例如:

<x-modal-form id="aRandomId" title="Test"></x-modal-form>

但在这种情况下,id aRandomId不会传播到 x-modal 组件。由于{{ $attributes }} ,我有一个错误“语法错误,意外'endif'(T_ENDIF),期待文件结束”

你知道怎么做吗?

4

2 回答 2

1

此行为已在 Laravel 8 中修复:#32576

您现在可以将$attributes变量从一个组件传递到另一个组件

于 2020-12-22T13:41:52.733 回答
-1

根据 laravel文件

所有不属于组件构造函数的属性都将自动添加到组件的“属性包”中。这个属性包通过 $attributes 变量自动提供给组件。通过回显此变量,可以在组件内呈现所有属性。

并且如果在一个视图中嵌套或使用多个组件并且想要使用属性包,因此您将公共属性放入此包中并手动放置唯一属性,如下所示:

<x-modal {{ $attributes }}>
    <x-modal-form {{ $attributes }} id="aRandomId" title="Test">
     // code here
    </x-modal-form>
</x-modal>

当然,id 属性为 HTML 元素指定了一个唯一的 id。id 属性的值在 HTML 文档中必须是唯一的。

于 2020-07-17T21:43:27.657 回答