在 livewire 中单击时无法刷新组件

时间:2021-04-24 12:44:23

标签: php laravel-8 laravel-livewire

尝试了所有可能的解决方案来刷新组件,但失败了。是否有任何选项不推送新评论并刷新 $comments 变量包含的任何内容?

想要在单击发送按钮时刷新 $comments 及其与刀片中回复的关系

在发送按钮上调用 $refresh 操作,但它似乎不起作用。甚至尝试使用 $this->render() 重新渲染组件。这也失败了。

<div>
    @foreach($comments as $item)
    <div class="row" @if($item->reply_id != null) style="margin-left:40px;" @endif>
        <div class="col-md-12">
            <div class="py-md-4 py-3 border_bottom" >
            <div class="row">
                <div class="col-md-1 col-sm-1 col-1">
                <div class="rating_person pt-3">
                    <img src="{{ isset($item->owner->profile_picture) ? asset($item->owner->profile_picture) : asset('images/default.jpg') }}">
                </div>
                </div>
                <div class="col-md-11 col-sm-12 col-12">
                <div class="rating_view_here_content_single_border">
                    <div class="rating_view_here_content_single">
                    <div class="rating_view_stars d-flex justify-content-between pt-3">
                        <div class="rating_view_heading">
                        <h4>{{ $item->owner->name }}</h4>
                        <div class="rating_wrapper">
                            <a href="#" class="grey_rating active-rating">
                            <i class="fa fa-star" aria-hidden="true"></i>
                            </a>
                            <a href="#" class="grey_rating active-rating">
                            <i class="fa fa-star" aria-hidden="true"></i>
                            </a>
                            <a href="#" class="grey_rating active-rating">
                            <i class="fa fa-star" aria-hidden="true"></i>
                            </a>
                            <a href="#" class="grey_rating active-rating">
                            <i class="fa fa-star" aria-hidden="true"></i>
                            </a>
                            <a href="#" class="grey_rating active-rating">
                            <i class="fa fa-star" aria-hidden="true"></i>
                            </a>
                        </div>
                        </div>
                        <span class="rating_days">{{ \App\Library\Helper::getTime($item->created_at) }}</span>
                    </div>
                    <div class="rating_comment_here pt-3">
                        <p>{{ $item->comment }}</p>
                        <p>
                        <a href="javascript:void(0)" class="reply" data-id="{{ $item->id }}" wire:click="toggleCommentBox({{ $item->id }}, 'true')">
                            <i class="fa fa-reply" aria-hidden="true"></i> <span>Reply</span>
                        </a>
                        </p>
                    </div>
                    @if($item_id == $item->id && $show=="true")
                        <div class="video_inner_form_wrapper commentBox-{{ $item->id }}">
                            <form wire:submit.prevent="sendReply" class="row">
                            <input type="hidden" name="reply_id" value="{{ $item->id }}">
                            <div class="comment_input col-md-8">
                            <input type="text" class="bluish_label video_inner_input @error('replyText') is-invalid @enderror" wire:model.lazy="replyText" placeholder="Write your comment here">
                            @error('replyText')
                            <div class="invalid-feedback">{{$message}}</div>
                            @enderror
                            </div>
                            <div class="col-md-4">
                                <button type="submit" class="btn_edit text-uppercase" wire:click="$refresh">Send</a>
                                <button type="button" class="btn_edit text-uppercase cancel" wire:click="toggleCommentBox({{ $item->id }}, 'false')">Cancel</a>
                            </div></form>
                        </div>
                    @endif
                    </div>
                </div> 
                </div>
            </div>
            </div>
            <livewire:frontend.discussion-chat :key="'chat-' . $item->id" :comments="$item->replies" :id="$model_id" :type="$model_type" />
        </div>
    </div>
    @endforeach
    </div>

1 个答案:

答案 0 :(得分:1)

由于没有更新任何属性,livewire 聊天组件将不会刷新。

可能的解决方案不是将列表作为参数传递,而是在聊天组件中获取它。使用事件触发刷新:

@click="$emit('discussion.chat.{{ $id }}.refresh')"

里面frontend.discussion-chat有一个

public function listeners(): array
{
    return [
        "discussion.chat.$this->id.refresh" => "reload"
    ];
}

public function reload(): void
{
    // E.g.
    $this->comments->fresh();
}