当前位置:首页 > 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,安装后直接使用。

vue 实现评分

安装:

npm install vue-star-rating

使用示例:

vue 实现评分

<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>

只读评分展示

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

<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
分享给朋友:

相关文章

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Desi…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue实现type切换

vue实现type切换

Vue 实现 Type 切换的实现方法 在 Vue 中实现 Type 切换功能可以通过动态组件、条件渲染或路由切换等方式实现。以下是几种常见方法: 使用 v-if 或 v-show 条件渲染 通过绑…

实现vue模板解析

实现vue模板解析

Vue 模板解析的基本原理 Vue 的模板解析是将模板字符串转换为渲染函数的过程。核心步骤包括模板编译、AST 生成、优化和代码生成。 模板编译阶段 Vue 使用 vue-template-comp…

vue实现路线导航

vue实现路线导航

Vue 实现路由导航的方法 在 Vue 中实现路由导航通常使用 Vue Router 库,它是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。以下是几种常见的实现方式: 安装 Vue…