如何在Livewire中手动刷新

问题描述 投票:0回答:1

在函数“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;
}
php laravel laravel-livewire
1个回答
0
投票

您可以创建一个

protected $listeners
数组并在事件触发时刷新,而不是使用 On 属性。

// In the shopping-cart component
protected $listeners = [
    'basketUpdated' => '$refresh'
];

public function basketUpdated(): void
{
    // something here
}
© www.soinside.com 2019 - 2024. All rights reserved.