【发布时间】:2021-03-02 19:40:32
【问题描述】:
我使用laravel 8 components,根据文档可以传递数据并使用它。但我需要一些修改传递的数据作为可以在组件中使用的变量
我的代码得到了未定义的变量:覆盖错误
<x-item_h :item="$item"/>
item-h.blade.php
<div class="item-h">
{{$item}}
{{$covers}}
</div>
item-h.php
class Item_h extends Component
{
public $item;
public $covers;
public function __construct($item )
{
$this->item = $item;
if ($item->getCover->count() > 0) {
$covers = $item->getCover;
} else {
$covers = $item->artists->getCover;
}
}
public function render()
{
return view('components.item_h');
}
}
那么如何在组件中定义可以在其中使用的变量呢?
谢谢
【问题讨论】:
-
您需要在
Item_h构造函数中使用$this->covers而不是$covers。 -
@remul 我之前测试过,没用
标签: php variables components laravel-8