我有一个 Laravel 7 组件,如下所示
class Input extends Component
{
public $name;
public $title;
public $value;
public $type = 'text';
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($name, $title)
{
$this->name = $name;
$this->title = $title;
$this->value = \Form::getValueAttribute($name);
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|string
*/
public function render()
{
return view('components.fields.input');
}
}
我可以像这样在 Blade 组件中渲染该字段:
<x-input name="name" :title="__('My field')" />
我需要在代码中创建和渲染该字段,我尝试了以下操作:
$field = new Input('name', 'My field');
$field->render();
这会返回一个错误:
Undefined variable: title
我可以看到渲染函数被调用,但公共属性不可用于视图。我将如何呈现具有公共属性的组件?
试试这个,它在 laravel 8 中对我有用,我检查过data
Laravel 7 中存在该函数
$field = new Input('name', 'My field');
$field->render()->with($field->data());
** 数据功能包括Component的方法、属性和属性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)