php - 如何在 laravel 组件中定义变量
问题描述
我使用laravel 8 组件,根据文档可以传递数据并使用它。但我需要一些修改传递的数据作为可以在组件中使用的变量
我的代码有未定义的变量:覆盖错误
<x-item_h :item="$item"/>
项目-h.blade.php
<div class="item-h">
{{$item}}
{{$covers}}
</div>
项目-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');
}
}
那么如何在组件中定义可以在其中使用的变量呢?
谢谢
解决方案
首先,当您像这样调用组件时,您需要添加封面。
<x-item_h :item="$item" covers/>
现在更新构造函数并添加覆盖您选择的默认值,这样即使您忘记给它赋值,它也不会使您的视图崩溃
class Item_h extends Component
{
public $item;
public $covers;
public function __construct($item ,$covers={{REPLACE WITH DEFAULT VALUE}})
{
$this->item = $item;
$this->covers = $covers;
if ($item->getCover->count() > 0) {
$covers = $item->getCover;
} else {
$covers = $item->artists->getCover;
}
}
public function render()
{
return view('components.item_h');
}
}
我相信这会解决你的问题
推荐阅读
- javascript - 如何在 React Native 中的 Alert 中添加 Lottie 动画
- swiftui - Clipped 实际上并不在 SwiftUI 中剪辑图像
- python - 我正在尝试将浮点数从元组分配给 numpy 数组
- javascript - 共享主机的MongoDB Atlas连接ip问题
- python - python mocks在'function'中找不到引用'assert_call_with'
- firebase - 如何从 Firebase 存储中获取图像 URL?
- wordpress - 'core/colums' innerBlocks 允许的块
- c++ - 'float' 和 'const c' 类型的无效操作数
- assembly - 如何实现 vpmovmskb 对 ZMM 寄存器的影响?
- r - 从具有 S4 元素的列表中提取值