提问者:小点点

无法让Livewire事件发射和收听工作


我正在尝试设置一个事件侦听器,以便当子livewire组件更新标题时,它将刷新父组件以显示更新,而不必硬刷新页面以查看更新。

这是一个快速的gif显示发生了什么

  • https://i.gyazo.com/faefb27c2fe0fb32da097fbbf5cc1acb.mp4

我有2个livewire组件。

父项=视图侧栏。php/view侧边栏。刀身php

// view-sidebar.blade.php
@foreach ($kanbans as $kanban )
   @livewire('kanbans.show-sidebar-kanban', ['kanban'=>$kanban], key($kanban->id))
@endforeach

// ViewSidebar.php
public $kanbans;

protected $listeners = ['refreshKanbans'];
  
public function refreshKanbans()
    {
        $this->kanbans = Kanban::where('status', $this->active)
                            ->orderBy('order', 'ASC')
                            ->get();
    }

public function mount()
    {
        $this->refreshKanbans();
    }

public function render()
    {
        return view('livewire.kanbans.view-sidebar');
    }

在子组件中,我设置了

public function updateKanban($id)
    {
        $this->validate([
            'title' => 'required',
        ]);

        $id = Kanban::find($id);

        if ($id) {
            $id->update([
                'title' => $this->title,
            ]);
        }

        $this->resetInputFields();

        $this->emit('refreshKanbans');

       
    }

显然,我对文档的理解是错误的,但不知道问题出在哪里。

任何帮助都非常感谢!

提前谢谢:)


共2个答案

匿名用户

你的代码有问题,让我来帮你。从子组件发出后(确保这做得很好)只需要在父组件中有这个

家长

protected $listeners = ['refreshKanbans' => '$refresh'];
  
public function render()
    {
        $this->kanbans = Kanban::where('status', $this->active)
                            ->orderBy('order', 'ASC')
                            ->get();
        return view('livewire.kanbans.view-sidebar');
    }

让我知道关于

匿名用户

我能够在子组件中使用它,并跳过发射。我能够添加DD,并且emit工作正常,但不确定它为什么没有更新。

public function updateKanban($id)
    {
        $this->validate([
            'title' => 'required',
        ]);

        $this->kanban->update(['title' => $this->title]);

        $this->resetInputFields();

        $this->kanban=Kanban::find($this->kanban->id);
       
    }