gpt4 book ai didi

laravel - 将参数传递给组件时出错

转载 作者:行者123 更新时间:2023-12-05 08:25:51 25 4
gpt4 key购买 nike

在这里阅读如何制作组件 https://laravel.com/docs/7.x/blade#components在我的 Laravel Framework 7.6.2 应用程序中出现错误:

Unresolvable dependency resolving [Parameter #0 [ <required> $is_auto_hide ]] in class App\View\Components\AppBackendHeader (View:

我在组件 app/View/Components/AppBackendHeader.php 中定义的第一个变量:

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class AppBackendHeader extends Component
{

public $is_auto_hide;

public $message;
public function __construct($is_auto_hide, $message)
{
$this->is_auto_hide = $is_auto_hide;
$this->message = $message;
}
public function render()
{
return view('components.app-backend-header');
}
}


calling this component from blade page :
<x-app-backend-header is_auto_hide="true" message="message 1234" />


In app/Providers/AppServiceProvider.php I added line :

public function boot()
{
Blade::component('app-backend-header', AppBackendHeader::class);
...

调用模板我传递了 is_auto_hide 参数,它看起来不像拼写错误......

我尝试运行命令:

$ composer dump-autoload 
$ php artisan view:clear
$ php artisan config:cache

这并没有帮助...有什么问题?

谢谢!

最佳答案

根据documentation :

Component constructor arguments should be specified using camelCase, while kebab-case should be used when referencing the argument names in your HTML attributes.

这意味着您需要:

public function __construct($isAutoHide, $message)
{
$this->is_auto_hide = $isAutoHide;
$this->message = $message;
}

你应该这样调用它:

 <x-app-backend-header is-auto-hide="true" message="message 1234" />

关于laravel - 将参数传递给组件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61359801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com