【发布时间】:2021-01-20 00:19:42
【问题描述】:
Livewire 如何在 <select> 更改 (wire:model) 上 $emit 事件
我需要在简单的<select> 更改上触发事件(从另一个组件中的数据库中获取一些数据)。
<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;
}
}
必须遗漏一些明显的东西。
【问题讨论】:
-
根本不起作用,尝试将
$this->emit('hallChanged')放在任何地方;D -
更改字段时是否输入
updatedHallId()方法?在该方法中添加dd('Test');或类似名称。 -
实际上不需要
wire:change,因为您只需从组件中侦听该属性的更新事件。 -
dd() in updatedHallId 确实有效,但这部分没有接缝: $this->emit('hallChanged', $value);或者 $listeners 可能有问题
-
两个组件都在同一个页面上?它们不能向其他用户或其他页面发出事件。您需要为此进行广播。