【发布时间】:2020-05-07 19:33:12
【问题描述】:
我正在尝试检查用户发表的每条评论和评论回复,并检查他们是否在 30 分钟前发表评论然后他们无法编辑它,但是我这样做的方式不起作用我收到了这个错误
[Vue 警告]:组件渲染函数中可能存在无限更新循环。
这是有问题的代码
<div v-for="comment in forum.comments">
<el-card shadow="never">
//check for other things
//set the comment info and layout
<div style="text-align: end" v-if="comment.user_id === $page.auth.user.auth.user.id">
{{minutes(comment)}}
<div v-if="editRemove === false" class="btn-link-edit action-button"
@click="edit(comment)">
<i class="fas fa-pencil-alt"></i>
</div>
</div>
<div v-for="reply in comment.replies">
//check for other things
//set the reply info and layout
<div style="text-align: end" v-if="reply.user_id === $page.auth.user.auth.user.id">
{{minutes(reply)}}
<div v-if="editRemove === false" class="btn-link-edit action-button"
@click="edit(reply)">
<i class="fas fa-pencil-alt"></i>
</div>
</div>
</div>
</el-card>
</div>
数据返回
editRemove: false,
方法
minutes(item) {
var minutes = moment().diff(moment(item.comment_time), 'minutes');
if (minutes >= 30) {
this.editRemove = true;
} else if (minutes <= 29) {
this.editRemove = false;
}
}
我应该怎么做才能解决这个问题?
【问题讨论】:
-
因为你有 2 个 if 并且没有默认情况,也许这就是原因,编译器会抱怨。尝试将
} else if (minutes <= 29) {更改为} else {。你也可以用this.editRemove=minutes >= 30写得更短 -
@thopaw 我改了,没区别
-
您有两个 v-for,您在模板 v-for 迭代中有一个函数,即
minutes(comment),您正在该函数中编辑相同的变量editRemove。所以你不断地改变同一个变量值。 -
@AJT82 因为我正在尝试使其成为动态的并且对于未知数量的 cmets 我将如何解决这个问题?
-
我会为 cmets 和回复制作哑组件(子组件),您可以使用计算属性来计算您的分钟数。无论如何,即使你不会遇到这个无限循环问题,它也永远不会只使用一个变量
editRemove,所有 cmets 和回复都需要有一个唯一标识符。
标签: vue.js