嗨,我正在尝试制作一个简单的刀片组件,但公共属性在视图中不可用,如文档所述:
班级
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 ]);
但结果是一样的 有人可以告诉我我做错了什么吗?
您提到的文档说:
您应该在其类构造函数中定义组件的所有数据属性。
并包含一个示例,表明:
/**
* Create the component instance.
*/
public function __construct(
public string $type,
public string $message,
) {}