提问人:albertosetim 提问时间:11/17/2023 更新时间:11/18/2023 访问量:29
在视图中找不到刀片组件中的公共属性
Public property in blade component not found in view
问:
嗨,我正在尝试制作一个简单的刀片组件,但公共属性在视图中不可用,如文档所述:
类
class ExampleComponent extends Component
{
public string $msg = "MSG";
/**
* Create a new component instance.
*/
public function __construct(){}
/**
* Get the view/contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.returned-products.create-returned-products-form');
}
}
视图
<div>
{{ $msg }}
</div>
R:未定义的变量$msg
我像这样尝试过:
return view('components.returned-products.create-returned-products-form', [ 'msg' => $this->msg ]);
但结果是一样的有人能告诉我我做错了什么吗?
答:
0赞
Don't Panic
11/17/2023
#1
在你提到的 Laravel Blade 文档的 Passing Data To Components 部分中,它说:
您应该在其类构造函数中定义组件的所有数据属性。组件上的所有公共属性将自动提供给组件的视图。
它还包括一个示例,显示:
/**
* Create the component instance.
*/
public function __construct(
public string $type,
public string $message,
) {}
评论