使用vant-ui+vue3实现一个可复用的评星组件

1111
如图所示 有两种情况 一种是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>

相关推荐

  1. 实现vant年月日时分秒组件

    2024-05-13 02:14:02       35 阅读
  2. uni-app + vant 实现搜索popup

    2024-05-13 02:14:02       34 阅读
  3. vant Circle 环形进度条写一个倒计时组件

    2024-05-13 02:14:02       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 02:14:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 02:14:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 02:14:02       87 阅读
  4. Python语言-面向对象

    2024-05-13 02:14:02       96 阅读

热门阅读

  1. E/CameraDeviceState: Cannot receive result while in state: 0

    2024-05-13 02:14:02       32 阅读
  2. 简单的Python示例母亲节的祝福

    2024-05-13 02:14:02       30 阅读
  3. tcpdump速查表

    2024-05-13 02:14:02       37 阅读
  4. springmvc处理模型数据

    2024-05-13 02:14:02       30 阅读
  5. 回到家萨嘎时间

    2024-05-13 02:14:02       29 阅读
  6. Map接口

    Map接口

    2024-05-13 02:14:02      27 阅读