首页 > 解决方案 > 未附加 vue 注释

问题描述

在我刷新页面之前,我的 Vue 代码不起作用。评论的回复成功进入数据库,但直到我刷新页面或添加评论然后刷新然后添加回复才显示在浏览器中。

评论.vue

我的方法

appendReply(comment,reply){
    _.find(this.comments, { id: comment.id }).children.push(reply)
},

已安装

bus.$on('comment:replied', ({ comment, reply }) => {
    this.appendReply(comment, reply)
    this.scrollToComment(reply)
})

我在控制台中收到此错误

[Vue 警告]:“comment:replied”的事件处理程序出错:“TypeError:无法读取未定义的属性‘push’”

标签: javascriptlaravelvue.js

解决方案


由于我知道您从哪里获得此代码以及流程是什么,因此我将为您提供问题的解决方案。

亚历克斯在解释appendReply方法时一直whenLoadedCommentResource回复(孩子们)。

要解决此问题,您需要转到后端提供的 Commentresource 类并将回复(子)属性设置为此代码:

'replies' => CommentResource::collection($this->replies)

或者在你的情况下

'children' => CommentResource::collection($this->children)

推荐阅读