我使用命令 php artisan make:component TestComponent
创建了一个 Laravel 组件
因此,创建了两个文件( resources/view/component/test-component.blade.php
和 app/View/Components/TestComponent.php
)。
但是,test-component.blade.php
文件工作正常,但是 TestComponent.php
文件被忽略。
我检查这个问题来更改渲染方法。
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class TestComponent extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public $id;
public function __construct($id="test")
{
$this->id = $id;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('components.foofoofoofoofoo'); // fake view name
}
}
假 View 名称(foofoofoofoofoo)被忽略,测试组件 View 被渲染。
当我使用像 <x-test-component />
这样的默认值时,id 变量不会传递到 View 。
( <x-test-component id='abc' />
工作得很好。)
我尝试在 AppServiceProvider.php
上的启动方法中手动注册
public function boot()
{
Blade::component('test-component', App\View\Components\TestComponent::class);
}
但是它也不起作用。
php artisan clear:view
, php artisan cache:clear
, composer dump
不适用于此问题。
最佳答案
我解决了这个问题,将laravel版本更新到8.38.0并运行php artisan view:clear
关于php - laravel 组件类被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67278770/