gpt4 book ai didi

laravel - Livewire 如何在选择更改时 $emit 事件(电线 :model)

转载 作者:行者123 更新时间:2023-12-03 23:03:22 27 4
gpt4 key购买 nike

Livewire 如何在 <select> 上 $emit 事件更改(电线:型号)
我需要在简单的 <select> 上触发事件(从另一个组件中的 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;
}
}

一定是遗漏了一些明显的东西。

最佳答案

使用 wire:change

<select id="hall" wire:model="hall_id" wire:change="change">...</select>
然后在组件中
在 ShowReservationForm.php
public function change()
{
$this->emit('hallChanged');
}
那么你可以在 ShowReservations收听组件引用链接 https://laravel-livewire.com/docs/2.x/events
在 ShowReservations.php
 protected $listeners = ['hallChanged' => 'change'];

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

关于laravel - Livewire 如何在选择更改时 $emit 事件(电线 :model),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64203446/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com