Laravel Livewire 发出或发出事件不触发

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

我正在使用 Laravel 10Livewire 2.x 我正在尝试使用

emitTo
组件中的简单函数中的发射或
Livewire
事件来更新愿望清单计数。

public function removeFromWishlist($id){
        $wishlist = Wishlist::findOrFail($id);
        $wishlist->delete();
        $this->emit('wishlistUpdated');
        session()->flash('message', 'Product Removed From Wishlist');
}

WishlistCount 控制器:

 protected $listeners = ['wishlistUpdated' => 'checkWishlistCount'];
    public $wishlistCount;

    public function checkWishlistCount(){
        if(Auth::check()){
            return $this->wishlistCount = Wishlist::where('user_id',Auth::user()->id)->count();
        }else{
            $this->wishlistCount = 0;
        }
    }

当我运行方法发出的函数时,它给我一个错误

enter image description here 方法发出不存在

另外,当我运行此代码时:

 $this->dispatchBrowserEvent('message', [
                'text' => 'Please Login To Continue',
                'type' => 'danger',
                'status' => 401
  ]);

它给了我同样的错误:方法

dispatchBrowserEvent
不存在。

每当我使用

$this->emit()
$this->emitTo()
$this->dispatchBrowserEvent()
或任何带有
$this->
的方法时 它给我一个错误,表明该方法不存在。

laravel events controller laravel-livewire
1个回答
0
投票

你必须使用这样的东西

 $this->dispatch('message', [
            'text' => 'Please Login To Continue',
            'type' => 'danger',
            'status' => 401

]);

在 livewire 3 emit 中,dispatchBrowserEvent 已更改为dispatch

© www.soinside.com 2019 - 2024. All rights reserved.