You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 10 + Livewire 2.x中emit、dispatchBrowserEvent等方法不存在的问题求助

Laravel 10 + Livewire 2.x中emit、dispatchBrowserEvent等方法不存在的问题求助

各位开发者朋友好,我目前正在用Laravel 10搭配Livewire 2.x开发项目,想要在Livewire组件的函数里通过emit或者emitTo事件来更新心愿单的数量,但碰到了一个头疼的问题。

先给大家展示下我的相关代码:

首先是移除心愿单的组件方法:

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;
    }
}

现在的问题是,当我运行removeFromWishlist函数时,会直接报错:

Method emit Doesn't exist

而且我尝试使用其他Livewire相关方法时,比如下面这段代码:

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

同样会触发错误,提示Method dispatchBrowserEvent doesn't exist。

总结来说,只要我在组件里用$this->emit()、$this->emitTo()、$this->dispatchBrowserEvent()这类Livewire内置方法,都会提示对应的方法不存在,有没有朋友能帮我排查下问题出在哪?

备注:内容来源于stack exchange,提问作者Abdulrhman Sayed Goda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 13:28:14