在函数“add”中的某个时刻,它会向组件“购物车”抛出事件。在“购物车”组件中,我想在收到事件时手动刷新该组件。在 livewire 网站上,它仅显示如何在组件视图上执行此操作,通过单击按钮,然后 livewire 处理其余部分,但这些功能似乎在后端不可用。不管怎样,还是我错过了什么??
//
// on the shopping-cart component
#[On('basketUpdated')]
public function basketUpdated(): void
{
// something here
}
// Adding more products to the Basket;
public function add($value): true|LaravelNotify
{
// Attempt to add the product to the use Basket DB
$addProductToBasket = $this->includeInBasket($value);
// checking for error
if (!$addProductToBasket)
return notify()->error('you are not connected, please connect and try again!', 'Could not update basket');
// Event to refresh shopping cart component
$this->dispatch('basketUpdated')->to('shopping-cart');
return true;
}
您可以创建一个
protected $listeners
数组并在事件触发时刷新,而不是使用 On 属性。
// In the shopping-cart component
protected $listeners = [
'basketUpdated' => '$refresh'
];
public function basketUpdated(): void
{
// something here
}