php - laravel 组件类被忽略

标签 php laravel

我使用命令 php artisan make:component TestComponent 创建了一个 Laravel 组件

因此,创建了两个文件( resources/view/component/test-component.blade.phpapp/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/

相关文章:

php读取目录文件

php - 全局访问模板中的用户对象

php - Sentry on Laravel 4. 无密码社交认证

Laravel 5.5 find($id) 返回集合而不是单个对象

Laravel API 护照 token

php - 尝试使用 PHP 和 XPath 删除目录中的 ID 匹配文件

php - 使用 PHP CLI 通过 imap 和 mysql 或其他语言处理电子邮件检索

php - 使用 ASIFormDataRequest 进行简单的 mysql 查询

php - 删除重复的记录,保留id较低的记录

laravel - 具有多列的 Eloquent WHERE LIKE 子句