提问者:小点点

Livewire如何在选择更改时发出$emit事件(wire:model)


Livewire如何在

我需要在简单的上触发事件(从另一个组件中的DB获取一些数据)

<select id="hall" wire:model="hall_id">...</select>

如何查看此模型的更改?在VueJS上,我们只设置了$watch或$computed属性,我相信livewire中也应该有类似的设置。奇怪的是为什么没有wire:change指令。

<?php

namespace App\Http\Livewire;

use App\Models\Event;
use App\Models\Hall;
use Livewire\Component;

class ShowReservationForm extends Component
{
    public $hall_id = '';

    protected $queryString = [
        'hall_id' => ['except' => ''],
    ];

    public function mounted()
    {
        //
    }

    public function updatedHallId($name, $value)
    {
        $this->emit('hallChanged', $value);
    }

    public function render()
    {
        return view('livewire.show-reservation-form', [
            'halls' => Hall::all(),
        ]);
    }

    public function getHallEventsProperty()
    {
        return Event::where('hall_id', $this->hall_id)->get();
    }
}

抓住它:

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class ShowReservations extends Component
{
    protected $listeners = ['hallChanged'];

    public $showTable = false;

    public function render()
    {
        return view('livewire.show-reservations');
    }

    public function hallChanged()
    {
        $this->showTable = true;
    }
}

一定错过了什么明显的东西。


共2个答案

匿名用户

使用线:更改

<select id="hall" wire:model="hall_id" wire:change="change">...</select>

然后在组件中

在ShowReservationForm.php

public function change()
{
     $this->emit('hallChanged'); 
}

然后您可以在ShowReservations组件ref链接上收听它https://laravel-livewire.com/docs/2.x/events

在保留区。php

 protected $listeners = ['hallChanged' => 'change'];

public function change()
{
   $this->showTable = true;
}

匿名用户

事实证明,在接收事件时,属性值不能是bool?

这是行不通的:

public $showTable = false;
...
public function hallChanged()
    {
        $this->showTable = true;
    }

这会奏效的

public $showTable = 0;
...
public function hallChanged()
    {
        $this->showTable = 1;
    }