Laravel Livewire:添加新记录后无法更新子组件

Laravel Livewire:添加新记录后无法更新子组件,laravel,laravel-livewire,Laravel,Laravel Livewire,我有个问题,我想不起来。我已经建立了一个组件结构,如下所示: 发布->评论->评论回复 CommentReply发出Post捕获的事件。Post更新评论集合 评论(模型)作为回应具有自我关系 现在,如果注释是顶级的,则视图将更新。但是,如果响应关系已更新,则视图不会显示更新。如果我发出一个刷新事件,该事件映射到注释(组件)中的$refresh,组件将抛出错误: Uncaught (in promise) TypeError: Cannot read property 'fingerprint'

我有个问题,我想不起来。我已经建立了一个组件结构,如下所示:

发布->评论->评论回复

CommentReply发出Post捕获的事件。Post更新评论集合

评论(模型)作为回应具有自我关系

现在,如果注释是顶级的,则视图将更新。但是,如果响应关系已更新,则视图不会显示更新。如果我发出一个刷新事件,该事件映射到
注释
(组件)中的
$refresh
,组件将抛出错误:

Uncaught (in promise) TypeError: Cannot read property 'fingerprint' of null
更新

CommentReply.php

public function post_reply () {
     ...
    $new_comment = $this->comment->response()->create($c);
    $this->is_replying = false;
    $this->emit('content:comments:replied', $new_comment);
}
<div>
@foreach($comment->responses as $response)
<div>
     <div>
         {{ $response->comment }}
     </div>

     <livewire:comment-reply :comment="$comment" :key="'comment-' . $response->id" />
</div>
@endforeach
</div>
<div x-data="{ replying: @entangle('is_replying') }">
    <button @click="replying = true;" x-show="!replying">Reply</button>
   <div x-show="replying">
       <textarea wire:model.defer="c.comment"></textarea>
       <button wire:click="post_reply">Post</button>
    </div>
</div>
    public $listeners = [
        'content:comments:replied' => 'replied'
    ];

   public function replied() {
        $this->responses = $this->comment->responses()->orderBy('created_at', 'desc')->get();
    }

<div>
    <livewire:comment-view :comment="$comment" />
    @foreach($responses as $r_comment) 
          <livewire:comment-view :comment="$r_comment" :key="'response-' . $r_comment->id" />
     @endforeach
</div>

Comment.php
(组件)

comment.blade.php

public function post_reply () {
     ...
    $new_comment = $this->comment->response()->create($c);
    $this->is_replying = false;
    $this->emit('content:comments:replied', $new_comment);
}
<div>
@foreach($comment->responses as $response)
<div>
     <div>
         {{ $response->comment }}
     </div>

     <livewire:comment-reply :comment="$comment" :key="'comment-' . $response->id" />
</div>
@endforeach
</div>
<div x-data="{ replying: @entangle('is_replying') }">
    <button @click="replying = true;" x-show="!replying">Reply</button>
   <div x-show="replying">
       <textarea wire:model.defer="c.comment"></textarea>
       <button wire:click="post_reply">Post</button>
    </div>
</div>
    public $listeners = [
        'content:comments:replied' => 'replied'
    ];

   public function replied() {
        $this->responses = $this->comment->responses()->orderBy('created_at', 'desc')->get();
    }

<div>
    <livewire:comment-view :comment="$comment" />
    @foreach($responses as $r_comment) 
          <livewire:comment-view :comment="$r_comment" :key="'response-' . $r_comment->id" />
     @endforeach
</div>


这就是我的经历

  • 创建了一个组件
    CommentView
    ,并将注释显示逻辑移动到此组件
    comment view.blade.php
  • comment.blade.php

    public function post_reply () {
         ...
        $new_comment = $this->comment->response()->create($c);
        $this->is_replying = false;
        $this->emit('content:comments:replied', $new_comment);
    }
    
    <div>
    @foreach($comment->responses as $response)
    <div>
         <div>
             {{ $response->comment }}
         </div>
    
         <livewire:comment-reply :comment="$comment" :key="'comment-' . $response->id" />
    </div>
    @endforeach
    </div>
    
    <div x-data="{ replying: @entangle('is_replying') }">
        <button @click="replying = true;" x-show="!replying">Reply</button>
       <div x-show="replying">
           <textarea wire:model.defer="c.comment"></textarea>
           <button wire:click="post_reply">Post</button>
        </div>
    </div>
    
        public $listeners = [
            'content:comments:replied' => 'replied'
        ];
    
       public function replied() {
            $this->responses = $this->comment->responses()->orderBy('created_at', 'desc')->get();
        }
    
    
    <div>
        <livewire:comment-view :comment="$comment" />
        @foreach($responses as $r_comment) 
              <livewire:comment-view :comment="$r_comment" :key="'response-' . $r_comment->id" />
         @endforeach
    </div>
    
    
    
    @foreach($r_评论形式的响应)
    @endforeach
    
    我就是这样做的

  • 创建了一个组件
    CommentView
    ,并将注释显示逻辑移动到此组件
    comment view.blade.php
  • comment.blade.php

    public function post_reply () {
         ...
        $new_comment = $this->comment->response()->create($c);
        $this->is_replying = false;
        $this->emit('content:comments:replied', $new_comment);
    }
    
    <div>
    @foreach($comment->responses as $response)
    <div>
         <div>
             {{ $response->comment }}
         </div>
    
         <livewire:comment-reply :comment="$comment" :key="'comment-' . $response->id" />
    </div>
    @endforeach
    </div>
    
    <div x-data="{ replying: @entangle('is_replying') }">
        <button @click="replying = true;" x-show="!replying">Reply</button>
       <div x-show="replying">
           <textarea wire:model.defer="c.comment"></textarea>
           <button wire:click="post_reply">Post</button>
        </div>
    </div>
    
        public $listeners = [
            'content:comments:replied' => 'replied'
        ];
    
       public function replied() {
            $this->responses = $this->comment->responses()->orderBy('created_at', 'desc')->get();
        }
    
    
    <div>
        <livewire:comment-view :comment="$comment" />
        @foreach($responses as $r_comment) 
              <livewire:comment-view :comment="$r_comment" :key="'response-' . $r_comment->id" />
         @endforeach
    </div>
    
    
    
    @foreach($r_评论形式的响应)
    @endforeach
    
    您需要查看循环中的关键元素,查看我几天前发布的类似内容,以及。我已经有了关键属性,请查看带有代码的更新问题。您需要查看循环中的关键元素,查看我几天前发布的类似内容,以及。我已经有了关键属性,请查看带有代码的更新问题。