Skip to content

Rate 评星

介绍

用于对事物进行评级操作的组件。

导入

js
import Rate from '@/components/form/Rate.vue'

用法

基础用法

通过 v-model 绑定评分值。

vue
<template>
  <Rate v-model="value" />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import Rate from '@/components/form/Rate.vue'

const value = ref(3)
</script>

自定义图标和颜色

通过 icon 和 voidIcon 设置选中和未选中的图标,通过 starActiveColor 和 starColor 设置颜色。

vue
<template>
  <Rate 
    v-model="value" 
    icon="clock-filling"
    voidIcon="stop"
    starActiveColor="primary"
    starColor="lightGrey"
  />
</template>

动态图标和颜色

可以根据评分值动态切换图标和颜色。

vue
<template>
  <Rate 
    v-model="value"
    :icon="dynamicIcon"
    voidIcon="cry"
    :starActiveColor="dynamicIconColor"
    starColor="lightGrey"
  />
</template>

<script setup lang="ts">
import Rate from '@/components/form/Rate.vue';
import { computed, ref } from 'vue';

const value = ref(1);
const dynamicIcon = computed(() => {
  if (value.value >= 4)
    return 'smile-filling';
  else if (value.value >= 2 && value.value < 4)
    return 'meh-filling';
  return 'cry-filling';
});
const dynamicIconColor = computed(() => {
  switch(value.value) {
    case 1:
      return '#FF4D4F';
    case 2:
      return '#FF9900';
    case 3:
      return '#FFCC00';
    case 4:
      return '#99CC33';
    case 5:
      return '#4caf50';
  }
});
</script>

自定义大小

通过 size 属性设置星星大小。

vue
<template>
  <Rate v-model="value" :size="80" />
</template>

可选择半星

通过 half 属性启用半星选择功能。

vue
<template>
  <Rate v-model="value" half />
</template>

自定义星星数量

通过 count 属性设置星星总数。

vue
<template>
  <Rate v-model="value" :count="10" />
</template>

禁用状态

通过 disabled 属性禁用评星组件。

vue
<template>
  <Rate v-model="value" disabled />
</template>

只读状态

通过 readonly 属性设置为只读,不可选择。

vue
<template>
  <Rate v-model="value" readonly />
</template>

API 参考

Rate

评星组件。

Props

名称说明类型必填默认值
v-model评星组件参数值number0
count星星的数量number5
canbeZero是否可以为0星booleanfalse
starStyle星星未激活自定义样式object-
starActiveStyle星星激活自定义样式object-
starActiveColor星星激活的颜色string'warning'
starColor星星未激活的颜色string'grey'
starDisableColor星星禁用的颜色string'grey'
icon选中时的图标string'favorite-filling'
voidIcon未选中时的图标string'favorite'
space星星之间的间距number3
half用户是否可以选择出半星booleanfalse
readonly是否只读booleanfalse
disabled评星组件是否禁用booleanfalse
size评星组件大小number48

Events

名称说明参数
update:modelValue评分变化时触发value: number

主题变量

名称类型默认值说明
RateStarActiveColorstring'warning'星星激活颜色
RateStarColorstring'grey'星星未激活颜色
RateStarDisableColorstring'grey'星星禁用颜色
RateIconstring'favorite-filling'选中图标
RateVoidIconstring'favorite'未选中图标
RateSpacenumber3星星间距
RateSizenumber48星星大小