【问题标题】:laravel livewire pass data between componentslaravel livewire 在组件之间传递数据
【发布时间】:2020-10-23 20:10:37
【问题描述】:

我有两个 livewire 组件

首先我有一个 foreach 循环

<ul>
    @foreach ($sectors as $sector)
        <li>{{ $sector->name }} <input type="submit" wire:click="showParent({{ $sector->id }})" class="btn btn-sm btn-primary" value="Adicionar"> </li>
        <ul class="mt-2">
        @foreach ($sector->children as $child)
                <li>{{ $child->name }}</li>
        @endforeach
        </ul>
    @endforeach
</ul>

这个循环显示扇区和父扇区,我有一个连线:单击以将数据传递给事件侦听器。 这很好用。

<?php

namespace App\Http\Livewire;

use App\Sector;
use Livewire\Component;

class SectorList extends Component
{
    public $construction;
    public $parent;

    public function mount($construction)
    {
        $this->construction = $construction;

    }

    public function showParent($id){
        $parent = Sector::find($id);
        $this->parent = $parent;
        $this->emit('newParent', $parent->id);
    }

    public function render()
    {
        $sectors = Sector::where('construction_id', $this->construction)->WhereNull('parent_id')->with('children')->get();

        return view('livewire.sector-list', compact('sectors'));
    }
}

第二个组件

<?php

namespace App\Http\Livewire;

use App\Sector;
use Livewire\Component;


class Sectors extends Component
{
    public $sector_name;
    public $parent_id = '';
    public $construction;
    public $segment;

    protected $listeners = ['newParent' => 'submit'];

    public function mount($construction)
    {
        $this->construction = $construction;

    }

    public function submit($id)
    {
        $this->parent_id = $id;
        //dd($this->parent_id = $id);
        
        $data = [
            'name' => $this->sector_name,
            'parent_id' => $this->parent_id = $id,
            'construction_id' => $this->construction,
            //'segment_id' => $this->segment
        ];
        Sector::Create($data);

    }

    public function render()
    {
        return view('livewire.sectors');
    }


}

这里的问题是,当我提交父 id 时保存为 null,当我双击 wire:click="showParent({{ $sector->id }})" 它被提交时没有名称

    <form wire:submit.prevent="submit">

        <input type="hidden" wire:model="construction" name="construction_id" value="{{ $construction }}">

        <div class="form-group">
            <label for="name">{{ trans('cruds.sector.fields.name') }}</label>
            <input  name="sector_name" id="sector_name" class="form-control" wire:model="sector_name">
            <span class="help-block">{{ trans('cruds.sector.fields.name_helper') }}</span>
        </div>
        <button type="submit" class="btn btn-primary">Salvar</button>
    </form>            
``````
`````
Can someone help??

Thanks

【问题讨论】:

  • 你确定这条线没问题:'parent_id' =&gt; $this-&gt;parent_id = $id, 在你的第二个组件的 $data 数组中?
  • 这让我得到了父母的 id,我在第一个组件中得到了它 $parent_id =Sector::find($id); $this->parent_id = $parent->id;但还是一样
  • 你能显示同时显示两个组件的标记吗?

标签: javascript laravel laravel-livewire


【解决方案1】:

问题出在 SectorList 组件控制器中,您不需要将变量 $sectors 作为视图参数传递,而是将其作为 public 放在类中,并以这种方式在 render 方法中声明值。

public function render()
    {
        $this->sectors = Sector::where('construction_id', $this->construction)->WhereNull('parent_id')->with('children')->get();

        return view('livewire.sector-list');
    }

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 2020-08-03
    • 2021-06-22
    • 1970-01-01
    • 2017-08-26
    • 2018-02-15
    • 2020-08-08
    • 2020-03-05
    • 1970-01-01
    相关资源
    最近更新 更多