Laravel 8 组件
Laravel 8 Components
我正在尝试创建一个组件,并向它发送一些数据。
<div class="card">
<div class="card-wrapper">
<div class="card-header">
<div class="card-title">
<i class="fa fa-pie-{{ $icon ?? '' }} fa-fw"></i>
<span>{{ $title ?? '' }}</span>
</div>
<div class="card-version">
<i class="fa fa-question-circle fa-fw"></i>
</div>
</div>
<div class="card-body">
{{ $content ?? '' }}
</div>
</div>
</div>
然后我使用
在 Blade 中调用该组件
<x-com-card icon="globe" title="Test"> </x-com-card>
但是我收到以下错误
未定义变量 $icon
我的组件控制器看起来像这样
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class ComCard extends Component
{
private $icon;
private $title;
/**
* Create a new component instance.
*
* @param $icon
* @param $title
*/
public function __construct($icon, $title)
{
$this->icon = $icon;
$this->title = $title;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('com.card');
}
}
知道为什么会这样吗?
这是因为您将属性设置为私有,视图无法访问它们。
When your component is rendered, you may display the contents of your
component's public variables by echoing the variables by name
public $icon;
public $title;
更多你可以在官方文档here中找到
我正在尝试创建一个组件,并向它发送一些数据。
<div class="card">
<div class="card-wrapper">
<div class="card-header">
<div class="card-title">
<i class="fa fa-pie-{{ $icon ?? '' }} fa-fw"></i>
<span>{{ $title ?? '' }}</span>
</div>
<div class="card-version">
<i class="fa fa-question-circle fa-fw"></i>
</div>
</div>
<div class="card-body">
{{ $content ?? '' }}
</div>
</div>
</div>
然后我使用
在 Blade 中调用该组件<x-com-card icon="globe" title="Test"> </x-com-card>
但是我收到以下错误
未定义变量 $icon
我的组件控制器看起来像这样
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class ComCard extends Component
{
private $icon;
private $title;
/**
* Create a new component instance.
*
* @param $icon
* @param $title
*/
public function __construct($icon, $title)
{
$this->icon = $icon;
$this->title = $title;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('com.card');
}
}
知道为什么会这样吗?
这是因为您将属性设置为私有,视图无法访问它们。
When your component is rendered, you may display the contents of your component's public variables by echoing the variables by name
public $icon;
public $title;
更多你可以在官方文档here中找到