從 Laravel 7 開始,我開始使用View Components。我正在嘗試將$attributes變量從一個組件傳遞到另一個組件,例如:x 模態分量:<div {{ $attributes->merge(['class' => 'modal fade']) }}>
Something great...
</div>x-模態形式組件:<x-modal {{ $attributes }}>
Something great too
</x-modal>在本例中,我在 x-modal 組件中有一個id屬性,例如:<x-modal-form id="aRandomId" title="Test"></x-modal-form>但在這種情況下,id aRandomId不會傳播到 x 模態組件。由于{{ $attributes }},我出現錯誤“語法錯誤,意外的 'endif' (T_ENDIF),期望文件結束”你知道怎么做嗎?
2 回答

料青山看我應如是
TA貢獻1772條經驗 獲得超8個贊
根據 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 文檔中必須是唯一的。
- 2 回答
- 0 關注
- 156 瀏覽
添加回答
舉報
0/150
提交
取消