【发布时间】:2023-04-02 01:27:01
【问题描述】:
所以基本上我有一个名为 Datatable 的组件。只是想为我的应用程序制作一个非常简单的表格组件。
当我调用视图时,错误似乎发生在组件文件的最后一步。
我的index.blade.php 我尝试使用这样的组件:
<x-datatable fn="users" />
为了明确,“fn”变量是我想在我的 Datatable 组件文件中调用的方法。
我的数据表组件文件如下所示:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Datatable extends Component
{
/**
* The function/metho we want to get
* data from at the datatable controller
*
* @var String
*/
private $fn;
/**
* The controller used
* DatatableController
*
* @var Controller
*/
private $controller = \App\Http\Controllers\DatatableController::class;
/**
* This is all the data the table needs
*
* @var JSON
*/
public $data;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($fn)
{
$this->fn = $fn;
}
/**
* Get the data in JSON format from the DataTable controller
* based on the method given in the x-blade component
*
* @return void
*/
public function data()
{
$this->data = app()->call([$this->controller, $this->fn]);
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('components.datatable');
}
}
我得到的错误是:
TypeError Argument 2 passed to Illuminate\View\Factory::startComponent() must be of the type array, null given, called in D:\Laragon\www\table-test\storage\framework\views\c3b84706f301f85254f4ebb5d2f86ff3d680d02b.php on line 5 (View: D:\Laragon\www\table-test\resources\views\index.blade.php)
我试图清除视图缓存,但它只是给出了同样的错误。
在$this->data = app()->call([$this->controller, $this->fn]); 上使用dd() 时,它会向我显示应有的数据。
组件使用的视图是空的,但它存在,所以那里也不应该有问题。
是什么原因造成的?有人见过这样的吗?
【问题讨论】:
-
也许您在没有 fn 的情况下在其他地方调用组件?您可以直接检查缓存视图文件,并在错误行之前手动添加 dd()。
标签: php laravel view components laravel-blade