【发布时间】:2020-11-12 08:16:57
【问题描述】:
我有一个无法解决的问题。我已将组件结构设置为:
发布 -> 评论 -> 评论回复
CommentReply 发出 Post 捕获的事件。发布更新 cmets 集合。
评论(模型)具有自我关系作为响应。
现在,如果评论是顶级的,则视图会更新。但是,如果响应关系被更新,则视图不会显示更新。如果我在Comment(组件)中发出一个映射到$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);
}
Comment.php(组件)
public $listeners = [
'content:comments:replied' => 'replied'
];
public function replied($comment) {
/*
received as array because type hinting
created error of array to string conversion
*/
$c = new CommentModel($comment);
$this->comment->responses->prepend($c);
}
comment.blade.php
<div>
@foreach($comment->responses as $response)
<div>
<div>
{{ $response->comment }}
</div>
<livewire:comment-reply :comment="$comment" :key="'comment-' . $response->id" />
</div>
@endforeach
</div>
comment-reply.blade.php
<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>
【问题讨论】:
-
您需要在一个循环中输入关键元素,请参阅我几天前发布的类似answer 以及official documentation on troubleshooting。
-
我已经有一个关键属性,请查看更新后的问题代码。