Livewire如何在<select>
更改(wire:model)时$emit事件
我需要在简单的<select>
更改时触发事件(从另一个组件中获取一些数据)。
<select id="hall" wire:model="hall_id">...</select>
如何观察此模型的变化?在VueJS中,我们只需设置$watch或$computed属性,我相信在livewire中也应该有类似的东西。奇怪的是为什么没有指令。这是我现在尝试发出事件的方式:
<?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;
}
}
一定是漏掉了什么显而易见的东西。
$this->emit('hallChanged')
;D - RomkaLTUupdatedHallId()
方法?在该方法中添加dd('Test');
或类似内容。 - Qirelwire:change
,因为您只需从组件中监听该属性的更新事件即可。 - Qirel