最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Method AppLivewirePostComment::emit does not exist - Stack Overflow

programmeradmin0浏览0评论

I use the Livewire framework in my Laravel, I created a ment feature, I want when I ment the screen will automatically scroll to the ment via the ID that I inserted using JavaScript but when I make a ment it should automatically scroll down but instead it appears error message "Method App\Livewire\Post\Comment::emit does not exist."

Please help me

 public function store()
    {
        $this->validate(["body" => "required"]);
        $ment = ModelsComment::create([
            'user_id' => Auth::user()->id,
            "article_id" => $this->article->id,
            "body" => $this->body
        ]);

        if($ment){
            $this->emit('mentStored', $ment->id);
            $this->body = null; 
        } else {
            session()->flash('danger', 'Komentar gagal Ditambahkan!!');
            return redirect()->route('data', $this->article->slug);
        }
    }

$this->emit('mentStored', $ment->id);

I searched on any site but no one discussed this error

I use the Livewire framework in my Laravel, I created a ment feature, I want when I ment the screen will automatically scroll to the ment via the ID that I inserted using JavaScript but when I make a ment it should automatically scroll down but instead it appears error message "Method App\Livewire\Post\Comment::emit does not exist."

Please help me

 public function store()
    {
        $this->validate(["body" => "required"]);
        $ment = ModelsComment::create([
            'user_id' => Auth::user()->id,
            "article_id" => $this->article->id,
            "body" => $this->body
        ]);

        if($ment){
            $this->emit('mentStored', $ment->id);
            $this->body = null; 
        } else {
            session()->flash('danger', 'Komentar gagal Ditambahkan!!');
            return redirect()->route('data', $this->article->slug);
        }
    }

$this->emit('mentStored', $ment->id);

I searched on any site but no one discussed this error

Share Improve this question edited May 7, 2024 at 16:05 Mohammad Salehi 7861 gold badge15 silver badges38 bronze badges asked May 7, 2024 at 8:01 devcodedevcode 211 silver badge3 bronze badges 2
  • Is your class extending from the Livewire Component class? – Mohammad Salehi Commented May 7, 2024 at 10:35
  • Hi there! Please read how to ask for more information about asking good questions on StackOverflow. Please add the full stack trace for the error and/or the code that generates the error. – rand'Chris Commented May 7, 2024 at 17:49
Add a ment  | 

2 Answers 2

Reset to default 4

In my case, upgrading from livewire 2.x to 3.x is easy:
$this->emit('updateHeaderOfForm'); //livewire 2.x
//is now
$this->dispatch('updateHeaderOfForm'); //livewire 3.x
NO MORE CHANGES
The solution is in the livewire documentation linked in the first answer of this post, more precisely at https://livewire.laravel./docs/upgrading#events

I suppose you are using Livewire 3.
In this version the emit() and dispatchBrowserEvent() methods have been unified into the dispatch() method.

The parameter must now be identified with the name assigned in the event handler:

$this->dispatch('mentStored', mentId: $ment->id);
namespace App\Livewire;
use Livewire\Attributes\On; // <~~~ note this

class someClass extends Component
{
    ....

    #[On('mentStored')] // <~~~ and this
    public function methodThatHandlesTheEvent(int $mentId) // <~~~ parameter name
    {
       dd ($mentId);
    }

    ....

发布评论

评论列表(0)

  1. 暂无评论