当前位置:首页 > uni-app

uniapp评论组件

2026-03-05 03:40:12uni-app

uniapp评论组件实现方法

评论列表渲染

使用v-for指令循环渲染评论列表,结合uni-listuni-list-item组件展示每条评论内容。示例代码:

<uni-list>
  <uni-list-item v-for="(item, index) in commentList" :key="index">
    <template v-slot:header>
      <view class="comment-header">
        <image :src="item.avatar" mode="aspectFill"></image>
        <text>{{item.nickname}}</text>
      </view>
    </template>
    <view class="comment-content">{{item.content}}</view>
    <template v-slot:footer>
      <view class="comment-footer">
        <text>{{item.createTime}}</text>
        <uni-icons type="chat" @click="replyComment(item)"></uni-icons>
      </view>
    </template>
  </uni-list-item>
</uni-list>

评论输入区域

通过uni-easyinputuni-section组件构建输入区域,底部固定定位确保随时可访问:

<view class="input-fixed">
  <uni-section title="发表评论" type="line">
    <uni-easyinput 
      v-model="commentContent" 
      placeholder="请输入评论内容" 
      :styles="inputStyle">
    </uni-easyinput>
    <button @click="submitComment">提交</button>
  </uni-section>
</view>

数据交互处理

在脚本部分实现评论提交和列表加载逻辑,注意处理分页和网络请求:

export default {
  data() {
    return {
      commentList: [],
      commentContent: '',
      page: 1,
      pageSize: 10
    }
  },
  methods: {
    async loadComments() {
      const res = await uni.request({
        url: '/api/comments',
        data: { page: this.page, pageSize: this.pageSize }
      })
      this.commentList = [...this.commentList, ...res.data.list]
    },
    async submitComment() {
      if(!this.commentContent.trim()) return
      await uni.request({
        url: '/api/comment',
        method: 'POST',
        data: { content: this.commentContent }
      })
      this.commentContent = ''
      this.page = 1
      this.loadComments()
    }
  },
  onLoad() {
    this.loadComments()
  }
}

样式优化

使用CSS增强交互体验,包括输入框固定、评论项间距等:

.input-fixed {
  position: fixed;
  bottom: 0;
  width: 100%;
  background: #fff;
  z-index: 999;
}
.comment-header {
  display: flex;
  align-items: center;
}
.comment-header image {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}
.comment-content {
  padding: 10px 0;
}

功能扩展建议

  • 添加图片上传功能,使用uni.chooseImage实现多图评论
  • 实现点赞功能,通过uni-icons显示点赞状态
  • 增加评论回复嵌套显示,使用递归组件处理层级关系
  • 添加@用户功能,配合textarea@input事件实现用户匹配
  • 集成emoji选择器,使用第三方插件如emoji-picker组件

性能优化方案

  • 实现虚拟列表加载,使用uni-listupper-thresholdlower-threshold属性
  • 添加数据缓存,通过uni.setStorage保存已加载评论
  • 使用节流函数控制频繁提交
  • 对长列表进行分片渲染,避免一次性渲染过多DOM节点

跨平台适配

  • 通过条件编译处理平台差异:

    // #ifdef H5
    const inputStyle = { backgroundColor: '#f5f5f5' }
    // #endif
    // #ifdef MP-WEIXIN
    const inputStyle = { backgroundColor: '#fff' }
    // #endif
  • 针对不同平台调整布局样式

  • 处理各平台键盘弹出事件差异,使用uni.onKeyboardHeightChange监听高度变化

    uniapp评论组件

标签: 组件uniapp
分享给朋友:

相关文章

vue实现折叠组件

vue实现折叠组件

Vue 折叠组件实现方法 基于 v-show 和事件触发 通过 v-show 控制内容的显示与隐藏,结合点击事件切换状态。 <template> <div> &…

uniapp教程

uniapp教程

uniapp 基础介绍 uniapp 是一个基于 Vue.js 的跨平台开发框架,支持一次开发,多端发布(如微信小程序、H5、App、快应用等)。其核心优势在于代码复用率高,学习成本低,适合快速构建多…

uniapp怎么使用

uniapp怎么使用

安装与开发环境搭建 下载HBuilderX作为开发工具,这是官方推荐的IDE,内置uniapp项目模板和调试工具。安装后通过新建项目选择uniapp模板,支持Vue.js语法。确保Node.js环境已…

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gree…

uniapp应用市场

uniapp应用市场

uniapp应用市场概述 Uniapp作为跨平台开发框架,支持将代码编译到iOS、Android、Web及各类小程序平台。其应用市场(插件市场)是开发者获取扩展插件、模板、组件的重要资源库,涵盖UI组…

uniapp使用npm

uniapp使用npm

uniapp中使用npm的方法 uniapp支持通过npm安装和管理第三方依赖包,以下是具体操作步骤: 安装Node.js环境 确保本地已安装Node.js(建议使用LTS版本),安装后会自动包含n…