简体   繁体   English

laravel 刀片组件视图中的变量未定义错误

[英]Variable undefined error in laravel blade component view

Component InputError.php组件输入错误.php

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class InputError extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

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

Blade input-error.blade.php刀片输入错误.blade.php

@props(['for'])

@error($for)
    <label {!! $attributes->merge(['class' => 'error']) !!}>
        {{ $message }}
    </label>
@enderror

Blade View叶片视图

<x-input-error for="title" />

Errors错误

Undefined variable: for未定义的变量:对于

i'm not want change original jetstream component, how can i fix it?我不想更改原始的 Jetstream 组件,我该如何修复它?

From the docs:从文档:

You should define the component's required data in its class constructor.您应该在其 class 构造函数中定义组件所需的数据。 All public properties on a component will automatically be made available to the component's view.组件上的所有公共属性将自动提供给组件的视图。 It is not necessary to pass the data to the view from the component's render method: (...)没有必要将数据从组件的渲染方法传递到视图:(...)

So, in your case:所以,在你的情况下:

class InputError extends Component
{
    public $for;

    public function __construct($for)
    {
        $this->for = $for;
    }

    public function render()
    {
        return view('components.input-error');
    }
}

Then you should be able to pass data:然后你应该能够传递数据:

<x-input-error for="my-title" />

PS: I think the issue is with the use of the @prop directive. PS:我认为问题在于使用@prop指令。 The directive is used in anonymous components that don't have a dedicated component class linked to the view.该指令用于没有链接到视图的专用组件 class 的匿名组件。 I almost always use anonymous components so I cannot be fully sure of this behaviour.我几乎总是使用匿名组件,所以我不能完全确定这种行为。

PS2: In your <label> tag use the {{ }} instead of {!! !!} PS2:在你的<label>标签中使用{{ }}而不是{!! !!} {!! !!}

input.php输入.php

class Input extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public $type;
    public $inputclass;
    public function __construct($inputclass=null,$color='primary')
    {
        $this->inputclass = $inputclass ?? "w-full px-4 py-2 border rounded-md dark:bg-darker dark:border-gray-700 focus:outline-none focus:ring focus:ring-$color-100 dark:focus:ring-$color-darker";
    }

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

input.blade.php输入.刀片.php

@props(['class'=>'','name'=>''])
<input 
    {{ $attributes->merge(['class' => $inputclass.' '.$class]) }}
/>
@error($name)
<span class="mt-2 text-sm text-red-600">{{ $message }}</span>
@enderror

to use component使用组件

<x-input
   color="secondary"
   wire:model.lazy="email"
   type="email"
   name="email"
   placeholder="Email address"
   equired
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM