【问题标题】:Unresolvable dependency resolving [Parameter #0 [ <required> string $message ]]无法解析的依赖解析 [Parameter #0 [ <required> string $message ]]
【发布时间】:2022-01-25 03:00:51
【问题描述】:

一旦他们在我的 edit.blade.php 文件中编辑了一个库存项目,我正试图在浏览器上弹出一条消息。我已经连接了路由器、控制器和视图文件,因此我可以编辑一个库存项目并在浏览器上查看更改,但我的消息说您成功编辑了库存并没有显示。 这是我的 InventoryForm.php 组件表单:

<?php

namespace App\View\Components;

use App\Models\Inventory;
use Illuminate\View\Component;

class InventoryForm extends Component
{
    /**
     * @var Inventory|null
     */
    public $inventory;

    /**
     * Successfully edited message
     *
     * @var string
     */
    public $message;

    /**
     * @param Inventory|null $inventory
     *
     * @param string $message
     */
    public function __construct(Inventory $inventory = null, string $message)
    {
        $this->inventory = $inventory;
        $this->message = $message;
    }

    /**
     * Get the view / contents that represent the component.
     *
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.inventory-form');
    }
}

这是我的 edit.blade.php 文件:

@extends('layouts.app')

@section('title', 'Create Inventory')

@section('menu')

@section('content')
    <h1><strong>Edit {{ $inventory->title }}</strong></h1>
    <x-inventory-form :inventory=$inventory/>
@endsection

当我将鼠标悬停在构造函数旁边的 (Inventory $inventory = null) 上时,它还会显示“在需要之前提供了可选参数。”

当我尝试编辑库存项目时,我收到此错误:

类 App\View\Components\InventoryForm 中无法解析的依赖解析 [Parameter #0 [ string $message ]](视图:/var/www/html/resources/views/pages/inventories/edit.blade.php)

任何帮助都会很棒!谢谢!!

【问题讨论】:

    标签: php laravel components


    【解决方案1】:

    &lt;x-inventory-form&gt; 标签中,:message 属性是必需的。因此,为了解决此问题,您可以:

    1. 在您的 InvetoryForm.php 中为构造函数参数添加默认值,因此消息属性不会重新

    public function __construct(Inventory $inventory = null, string $message = null)

    1. 在刀片文件中添加属性 &lt;x-inventory-form :inventory="$inventory" :message="$message"&gt; 或将其传递给字符串: &lt;x-inventory-form :inventory="$inventory" message="some massage"&gt;

    参考地址:https://laravel.com/docs/8.x/blade#components

    【讨论】:

      猜你喜欢
      • 2014-07-30
      • 2019-04-23
      • 2019-08-23
      • 2019-05-25
      • 2018-05-11
      • 2021-07-21
      • 1970-01-01
      • 2021-06-12
      • 2015-12-25
      相关资源
      最近更新 更多