以编程方式渲染Laravel 7组件 [英] Render Laravel 7 component programmatically
问题描述
我有一个看起来像这样的Laravel 7组件
I have a Laravel 7 component which looks like this
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组件中渲染字段,如下所示:
I can render the field in my Blade component like this:
< x输入名称=名称":title ="__('我的字段')"/>
我需要在代码中创建和呈现该字段,我尝试了以下操作:
I have a requirement to create and render the field in code, I've tried the following:
$field = new Input('name', 'My field');
$field->render();
这将返回错误:
未定义的变量:标题
我看到调用了render函数,但是公共属性对视图不可用.我将如何使用公共属性渲染组件?
I can see that the render function is called but the public properties are not made available to the view. How would I render the component with the public properties?
推荐答案
手动将变量添加到视图中.否则它将无法正常工作
Manually add variables to the view. Otherwise it won't work
我报告了这个,但是这不算问题:
I reported this but it's not considered an issue:
https://github.com/laravel/framework/issues/32429
这篇关于以编程方式渲染Laravel 7组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!