Laravel Livewire 同一组件两个项目在一页同步

Laravel Livewire Same Component Two Items Sync in One Page

我制作了follower个组件。

如果访问者关注作者,组件会显示Following,如果没有,会显示Follow.

我在页面的两个地方放置了follower组件。

-跟随者组件。

<div>
    <a href="javascript:void(0);" class="follow_btn" wire:click="toggle">{{$following? 'Following': 'Follow'}}</a>
</div>

public $关注 = 真; public$post;

public function mount($post)
{
    $this->post = $post;
}
public function render()
{
    if(Auth::check()&&user()->isFollowing($this->post->user))
    {
        $this->following = true;
    }else {
        $this->following = false;
    }
    return view('livewire.following');
}
public function toggle()
{
    user()->toggleFollow($this->post->user);
}

我在一个页面的两个地方使用了这个组件。

<livewire:follower :post="$post">
...
<livewire:follower :post="$post">

当我单击一个组件时,它被切换并且运行良好。

但其他组件保持旧状态。

如何同步它们?

谢谢

我不得不发出刷新事件。

protected $listeners = [
     'followingRefresh' => '$refresh',
];

public function mount($post)
{
    $this->post = $post;
}
public function render()
{
    if(Auth::check()&&user()->isFollowing($this->post->user))
    {
        $this->following = true;
    }else {
        $this->following = false;
    }
    return view('livewire.following');
}
public function toggle()
{
    user()->toggleFollow($this->post->user);
    $this->emit('followingRefresh'); //emit event
}
    

希望这对某人有所帮助。