当前位置:首页 > VUE

vue 实现评分

2026-02-10 09:42:46VUE

实现评分组件的方法

在Vue中实现评分功能,可以通过自定义组件或使用第三方库来完成。以下是几种常见的实现方式:

使用自定义组件

创建一个可交互的评分组件,允许用户点击星星进行评分。

<template>
  <div class="star-rating">
    <span 
      v-for="star in 5" 
      :key="star" 
      @click="setRating(star)"
      :class="{ 'active': star <= currentRating }"
    >
      ★
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentRating: 0
    }
  },
  methods: {
    setRating(star) {
      this.currentRating = star
      this.$emit('rating-selected', star)
    }
  }
}
</script>

<style>
.star-rating span {
  font-size: 24px;
  color: #ccc;
  cursor: pointer;
}
.star-rating span.active {
  color: gold;
}
</style>

使用第三方库

可以使用现成的评分组件库,如vue-star-rating,安装后直接使用。

安装:

npm install vue-star-rating

使用示例:

<template>
  <star-rating 
    v-model="rating"
    :increment="0.5"
    :star-size="20"
  />
</template>

<script>
import StarRating from 'vue-star-rating'

export default {
  components: {
    StarRating
  },
  data() {
    return {
      rating: 0
    }
  }
}
</script>

实现半星评分

如果需要支持半星评分,可以修改自定义组件逻辑。

<template>
  <div class="star-rating">
    <span 
      v-for="star in 5" 
      :key="star" 
      @click="setRating(star)"
      @mousemove="setTempRating(star)"
      @mouseleave="resetTempRating"
      :class="getStarClass(star)"
    >
      ★
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentRating: 0,
      tempRating: 0
    }
  },
  methods: {
    setRating(star) {
      this.currentRating = star
    },
    setTempRating(star) {
      this.tempRating = star
    },
    resetTempRating() {
      this.tempRating = 0
    },
    getStarClass(star) {
      return {
        'active': star <= this.currentRating,
        'hover': star <= this.tempRating && this.tempRating > 0
      }
    }
  }
}
</script>

<style>
.star-rating span {
  font-size: 24px;
  color: #ccc;
  cursor: pointer;
  transition: color 0.2s;
}
.star-rating span.active {
  color: gold;
}
.star-rating span.hover {
  color: orange;
}
</style>

只读评分展示

如果只需要展示评分而不需要交互,可以使用简单的渲染逻辑。

vue 实现评分

<template>
  <div class="star-rating">
    <span 
      v-for="star in 5" 
      :key="star" 
      :class="{ 'active': star <= rating }"
    >
      {{ star <= rating ? '★' : '☆' }}
    </span>
  </div>
</template>

<script>
export default {
  props: {
    rating: {
      type: Number,
      required: true
    }
  }
}
</script>

<style>
.star-rating span {
  font-size: 20px;
}
.star-rating span.active {
  color: gold;
}
</style>

以上方法可以根据具体需求选择使用,自定义组件提供了最大的灵活性,而第三方库则可以快速实现复杂功能。

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

相关文章

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现单据

vue实现单据

Vue 实现单据功能的方法 使用 Vue 实现单据功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方案: 表单设计与数据绑定 使用 Vue 的 v-model 指令实现表单数据的双向…

vue单选实现

vue单选实现

Vue 单选实现 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选效果。当用户选择不同的选…

vue 实现协同

vue 实现协同

Vue 实现协同编辑的方案 协同编辑指多个用户同时编辑同一文档并实时同步更改。Vue 结合相关库和技术可轻松实现该功能。 使用 WebSocket 实现实时通信 WebSocket 提供全双工通信,…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…