如图所示 有两种情况 一种是5颗星 一种是3颗星
官网上只提供了图标类型的 并没有加文字
https://femessage-vant.netlify.app/#/zh-CN/
自己结合两种情况
在全局注册了此组件(后续还会持续更新代码~)
<template>
<div class="vant_rate_wrapper">
<van-rate class="vant_rate" :class="{'vant_rate_3': props.count===3}" :disabled="props.disabled" color="#F4BA43" void-icon="star" void-color="#EAEAEA" :count="props.count" v-model="data.rateValue" />
<div class="vant_rate_tip" v-if="props.count===5">
<span class="very_dissatisfied" :class="{'text-gray': data.rateValue < 1}">非常不满意</span>
<span class="dissatisfied" :class="{'text-gray': data.rateValue < 2}">不满意</span>
<span class="general" :class="{'text-gray': data.rateValue < 3}">一般</span>
<span class="pleased" :class="{'text-gray': data.rateValue < 4}">满意</span>
<span class="very_pleased" :class="{'text-gray': data.rateValue < 5}">非常满意</span>
</div>
<div class="vant_rate_tip_three" v-else>
<span class="very_dissatisfied" :class="{'text-gray': data.rateValue < 1}">非常不满意</span>
<span class="general" :class="{'text-gray': data.rateValue < 2}">一般</span>
<span class="pleased" :class="{'text-gray': data.rateValue < 3}">满意</span>
</div>
</div>
</template>
<script setup lang="ts">
import {ref, reactive, watch} from "vue";
//props
interface iProps {
touchable?:boolean, //是否可以通过滑动手势选择评分
count?:number
disabled?: boolean
error?:boolean
modelValue: number
[key: string]: any
}
//emit
interface iEmit {
(e: 'click', value: string): void
(e: 'input', value: string): void
(e: 'change', value: string): void
(e: 'update:modelValue', value: number): void //更新v-model
}
const emit = defineEmits<iEmit>();
const props = withDefaults(defineProps<iProps>(), {
modelValue:0
})
interface iData {
rateValue:number,
}
const data:iData=reactive({
rateValue:props.modelValue
})
watch(()=>props.modelValue,(newVal)=>{
console.log(44434,newVal)
data.rateValue = newVal
},{
immediate: true
})
</script>
<style scoped lang="less">
.vant_rate_wrapper{
width: calc(100% - 40px);
height: 46px;
border-radius: 5px;
padding: 20px ;
border: 1px solid #DDE0E8;
/deep/.vant_rate{
display: flex;
justify-content: space-around;
.van-rate__item{
margin-left: 5px;
}
}
/deep/.vant_rate_3{
display: flex;
justify-content: space-around;
padding: 0 14px 0 20px;
.van-rate__item {
&:first-child {
margin-left: 0; // 第一个星与五个星的第一个星对齐
}
&:nth-child(2) {
margin-left: 36%; // 第二个星与五个星的第三个星对齐
}
&:nth-child(3) {
margin-left: auto; // 第三个星与五个星的最后一个星对齐
}
}
}
.vant_rate_tip{
margin-top: 14px;
line-height: 1;
display: flex;
justify-content: flex-start;
text-align: left;
span{
font-weight: 400;
font-size: 12px;
color:rgba(0,0,0,0.9)
}
.dissatisfied{
margin-left: 10px;
}
.general{
margin-left: 26px;
}
.pleased{
margin-left: 36px;
}
.very_pleased{
margin-left: 22px;
}
}
.vant_rate_tip_three{
margin-top: 14px;
line-height: 1;
display: flex;
justify-content: flex-start;
text-align: center;
span{
font-weight: 400;
font-size: 12px;
}
.general{
margin-left: 70px;
}
.pleased{
margin-left: 97px;
}
}
/deep/.van-rate--disabled{
.van-rate__icon--full{
color:#F4BA43;
}
}
.text-gray {
color: rgba(0,0,0,0.35) !important; // 灰色
}
}
</style>