Skip to content

comment滚动加载有重复值 #110

Description

@jainnieh

这是我看的第一个完整的前端vue项目,可能因为我的知识点不够多, 没有思考完整. 如果是我知识点的缺漏, 请指出.
我在本地搭建前后端运行发现一个报错.
代码位于comment.vue中, 报错提示vue的v-for绑定的:key="item.commentId"重复:

// 滚动加载事件
    pullUp() {
      console.log('getComment')
      getComment(this.$route.params.id, this.page).then(({ comments }) => {
        this.commentList = [...this.commentList, ...comments]
        //测试输出
        console.log(this.commentList)
        console.log(comments)
      })
    }

我思考了原因, 如下:
以上逻辑应该是按照页数获取数据库中评论记录. 但是数据库中的记录会不断增加, 举个例子: 第一次加载评论数据库中[10, 9, 8, ...] 的前5条为[10, 9, 8, 7, 6] . 等第二次请求发出, 数据库可能出现新评论, 如果数据库更新为[11, 10, 9, 8, ...] ,此时请求第二页就得到[6, 5, 4, 3, 2], 显然两次请求中6重复了.
pullUp()中使用 [...this.commentList, ...comments]进行合并, 应该不会过滤重复, 因为两个数组中都是放的对象, 对象检测的是地址值,不是内容.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions