使用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>
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用vant-uploader和vue-cropper来实现裁剪图片并上传,你可以按照以下步骤进行操作: 1. 首先,安装vantvue-cropper插件。你可以使用npm或yarn来安装它们。 2. 在你的Vue组件中,引入vant-uploader和vue-cropper组件。 3. 在模板中,使用vant-uploader组件实现图片上传功能。设置上传的action属性为你的上传接口地址,并设置on-success事件来处理上传成功后的逻辑。 4. 在on-success事件中,获取到上传成功后的图片地址,并将其传递给vue-cropper组件。 5. 在vue-cropper组件中,设置裁剪框的样式和裁剪比例等属性。使用v-model指令来绑定裁剪后的图片数据。 6. 在提交按钮的点击事件中,将裁剪后的图片数据上传到服务器。 下面是一个的示例代码: ```vue <template> <div> <van-uploader action="/upload" :on-success="handleUploadSuccess" ></van-uploader> <vue-cropper v-if="showCropper" :src="cropperSrc" :output-size="{ width: 200, height: 200 }" :output-type="'jpeg'" :fixed-box="true" :fixed-number="\[1, 1\]" v-model="croppedImage" ></vue-cropper> <button @click="handleSubmit">提交</button> </div> </template> <script> import { VanUploader } from 'vant'; import VueCropper from 'vue-cropper'; export default { components: { VanUploader, VueCropper, }, data() { return { showCropper: false, cropperSrc: '', croppedImage: '', }; }, methods: { handleUploadSuccess(response) { // 获取上传成功后的图片地址 const imageUrl = response.data.imageUrl; // 显示裁剪组件 this.showCropper = true; // 设置裁剪组件的图片地址 this.cropperSrc = imageUrl; }, handleSubmit() { // 提交裁剪后的图片数据到服务器 // this.croppedImage 包含裁剪后的图片数据 }, }, }; </script> ``` 请注意,以上代码只是一个的示例,你需要根据你的实际需求进行适当的修改和调整。同时,你还需要在后端实现相应的上传和裁剪功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值