(二十二)、实现评论功能(2)【uniapp+uinicloud多用户社区博客实战项目(完整开发文档-从零到完整项目)】

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 (二十二)、实现评论功能(2)【uniapp+uinicloud多用户社区博客实战项目(完整开发文档-从零到完整项目)】,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1,渲染评论列表

1.1,在detail页面中定义评论列表数组和getcomment方法:
commentList: [],

getcomment方法:

      //获取评论列表
      async getComment() {
        let commentTemp = db.collection("quanzi_comment")
          .where(`article_id == "${this.artid}"`).orderBy("comment_date desc")
          .limit(20).getTemp();
        let userTemp = db.collection("uni-id-users").field("_id,username,nickname,avatar_file").getTemp()

        let res = await db.collection(commentTemp, userTemp).get()
        console.log(res.result.data)
        this.commentList = res.result.data
      },
1.2,父组件传值给子组件(detail—comment-item)

detail中:

      <!-- 评论内容子组件 -->
      <view class="content">
        <view class="item" v-for="item in commentList">
          <comment-item :item="item"></comment-item>
        </view>
      </view>

comment-item子组件接收数据:

    props: {
      item: {
        type: Object,
        default () {
          return {}
        }
      }
    },
1.3,渲染数据到页面

这里需要引入以前封装的两个方法 giveName,giveAvatar

  import {
    giveName,
    giveAvatar
  } from "../../utils/tools.js"

渲染数据到页面:

  <view>
    <view class="comment-item">
      <view class="avatar">
        <u-avatar :src="giveAvatar(item)" size="26"></u-avatar>
      </view>

      <view class="wrap">
        <view class="username">{{giveName(item)}}</view>
        <view class="comment-content">{{item.comment_content}}</view>
        <view class="info">
          <view class="reply-btn">3回复 </view>
          <view>
            <uni-dateformat :date="item.comment_date" :threshold="[60000,3600000*24*30]">
            </uni-dateformat>
          </view>
          <view>{{item.province}}</view>
        </view>
      </view>
    </view>
  </view>
1.4,效果:

在这里插入图片描述

2,评论后及时无感展示以及细节处理

2.1,评论无感展示

定义以下属性:

 noComment: false

修改页面:

      <!-- 暂无评论 -->
      <view style="padding-bottom:50rpx" v-if="!commentList.length && noComment">
        <u-empty mode="comment" icon="http://cdn.uviewui.com/uview/empty/comment.png">
        </u-empty>
      </view>

修改方法:

        let res = await db.collection(commentTemp, userTemp).get()
        console.log(res.result.data)
        if (res.result.data == 0) this.noComment = true
        this.commentList = res.result.data
2.2,评论成功后的回调

在子组件发布评论的方法中,添加以下代码:

          //给父组件传递一个函数
          this.$emit("commentEnv", {
            _id: res.result.id,
            comment_content: this.replyContent,
            "province": province,
            comment_date: Date.now()
          })

在父组件detail中接收并打印输出:

    <!-- 评论输入框子组件 -->
    <comment-frame :commentObj="commentObj" @commentEnv="PcommentEnv"></comment-frame>

commentEnv方法:

      //评论成功后的回调
      PcommentEnv(e) {
        console.log(e);
        this.commentList.unshift({
          ...this.commentObj,
          ...e,
          user_id: [store.userInfo]
        })
      },

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135721.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!