当前位置:首页 > VUE

vue实现评价

2026-03-07 15:08:47VUE

Vue 实现评价功能

基本组件结构

使用 Vue 的单文件组件(SFC)实现评价功能,核心结构包括评分控件和评价表单。

<template>
  <div class="rating-container">
    <h3>发表评价</h3>
    <div class="stars">
      <span 
        v-for="star in 5" 
        :key="star"
        @click="setRating(star)"
        :class="{ 'active': star <= currentRating }"
      >★</span>
    </div>
    <textarea v-model="comment" placeholder="请输入评价内容"></textarea>
    <button @click="submitRating">提交评价</button>
  </div>
</template>

数据与交互逻辑

通过 Vue 的响应式数据管理评分状态和评价内容。

vue实现评价

<script>
export default {
  data() {
    return {
      currentRating: 0,
      comment: ''
    }
  },
  methods: {
    setRating(rating) {
      this.currentRating = rating
    },
    submitRating() {
      if (this.currentRating === 0) {
        alert('请选择评分')
        return
      }
      console.log('提交评价:', {
        rating: this.currentRating,
        comment: this.comment
      })
      // 实际项目中此处调用API提交数据
      this.resetForm()
    },
    resetForm() {
      this.currentRating = 0
      this.comment = ''
    }
  }
}
</script>

样式设计

使用 CSS 增强交互视觉效果,例如悬停状态和选中状态。

vue实现评价

<style scoped>
.rating-container {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
}
.stars span {
  font-size: 2rem;
  color: #ccc;
  cursor: pointer;
  transition: color 0.2s;
}
.stars span.active,
.stars span:hover {
  color: #ffcc00;
}
.stars span:hover ~ span {
  color: #ccc;
}
textarea {
  width: 100%;
  min-height: 100px;
  margin: 15px 0;
}
button {
  background: #42b983;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
}
</style>

进阶功能实现

对于需要显示历史评价的场景,可以添加评价列表组件。

<template>
  <div class="review-list">
    <div v-for="(review, index) in reviews" :key="index" class="review-item">
      <div class="review-rating">
        <span v-for="star in 5" :key="star" :class="{ 'active': star <= review.rating }">★</span>
      </div>
      <p class="review-content">{{ review.comment }}</p>
      <p class="review-time">{{ review.time }}</p>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    reviews: {
      type: Array,
      default: () => []
    }
  }
}
</script>

服务端交互

实际项目中通过 axios 与后端 API 通信,示例为提交评价的异步操作。

methods: {
  async submitRating() {
    try {
      const response = await axios.post('/api/ratings', {
        rating: this.currentRating,
        comment: this.comment
      })
      this.$emit('rating-submitted', response.data)
      this.resetForm()
    } catch (error) {
      console.error('评价提交失败:', error)
    }
  }
}

注意事项

  • 使用 v-model 实现表单双向绑定
  • 通过计算属性实现派生数据(如平均分)
  • 考虑添加表单验证逻辑
  • 对于敏感操作(如删除评价)需要添加确认对话框

以上实现可以根据实际需求进行扩展,例如添加图片上传、多维度评分等功能。

标签: 评价vue
分享给朋友:

相关文章

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue实现画板

vue实现画板

Vue 实现画板功能 在 Vue 中实现画板功能可以通过 HTML5 的 Canvas API 实现。以下是一个完整的实现方案: 创建 Vue 组件 新建一个 Vue 单文件组件(SFC),命名为…