封装了一个element-UI里面的星星等级组件

效果

父组件

<template>
	<div class="container">
		<h3>评价</h3>{{starIndex}}<div class="box">
			<Rate v-model:star-index="starIndex" />
		</div>
	</div>
</template>
<script setup>
import { ref } from "vue";
import Rate from '@/components/Rate.vue';
const starIndex = ref(1)

</script>

<style spoted>
.box {
	display: flex;
	justify-content: flex-start;
}
</style>

子组件

<template>
	<span v-for="(_,i) in starNums" @mouseover="moveChange(i)" @mouseleave="leaveChange" @click="handleClick(i)"
		:class="[{active:isChangeStarNum >= i},'icon']">
	</span>
	<span>{{evaluation}}</span>
</template>

<script setup>
import { ref, computed } from 'vue';
// 对外暴露结果
const emit = defineEmits(['update:starIndex'])
// 可接参 
const props = defineProps({
	// 星星数量 默认: 5
	starNums: {
		type: Number,
		default: 5,
	},
	// 评价列表 默认:['极差','失望','一般','满意','惊喜']
	evaluationList: {
		type: Array,
		default: ['极差', '失望', '一般', '满意', '惊喜'],
	},
	// v-model:starIndex
	starIndex: {
		type: Number,
		default: 0,
		required: true
	}
})
const isChangeStarNum = ref(props.starIndex - 1);
const isLeaveCondition = ref(0)
// 鼠标移动
const moveChange = (i) => {
	// 阻止条件
	if (isLeaveCondition.value) return
	isChangeStarNum.value = i
}
// 鼠标移出
const leaveChange = () => {
	// 阻止条件
	if (isLeaveCondition.value) return
	isChangeStarNum.value = -1
}
// 评价
const evaluation = computed(() => props.evaluationList[isChangeStarNum.value])
// 点击
const handleClick = (i) => {
	isChangeStarNum.value = i
	// 点击后阻止鼠标移动事件
	isLeaveCondition.value = 1
	// 对外暴露结果
	emit('update:starIndex', isChangeStarNum.value + 1)
}
</script>

<style scoped>
.icon {
	position: relative;
	height: 24px;
	width: 24px;
	background: url(@/assets/img/star.png) no-repeat;
}

.active {
	background-position: 0px -25px;
}
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
label、value、type、options等,用于渲染表单元素。同时支持表单验证和表单提交。 示例代码: ```vue <template> <el-form ref="form" :model="form" :rules="rules" label-width="100px"> <el-form-item v-for="item in items" :key="item.label" :label="item.label" :prop="item.value"> <el-input v-if="item.type === 'text'" v-model.trim="form[item.value]" :placeholder="item.placeholder"></el-input> <el-select v-else-if="item.type === 'select'" v-model.trim="form[item.value]" :placeholder="item.placeholder"> <el-option v-for="option in item.options" :key="option.value" :label="option.label" :value="option.value"></el-option> </el-select> <el-date-picker v-else-if="item.type === 'date'" v-model.trim="form[item.value]" :placeholder="item.placeholder" type="date"></el-date-picker> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> </el-form-item> </el-form> </template> <script> export default { props: { items: { type: Array, required: true } }, data() { return { form: {}, rules: {} } }, mounted() { this.items.forEach(item => { this.form[item.value] = '' if (item.required) { this.rules[item.value] = [{ required: true, message: item.label + '不能为空', trigger: 'blur' }] } }) }, methods: { submitForm() { this.$refs.form.validate(valid => { if (valid) { this.$emit('submit', this.form) } }) } } } </script> ``` 使用方式: ```vue <template> <dynamic-form :items="formItems" @submit="handleSubmit"></dynamic-form> </template> <script> import DynamicForm from './DynamicForm.vue' export default { components: { DynamicForm }, data() { return { formItems: [ { label: '用户名', value: 'username', type: 'text', placeholder: '请输入用户名', required: true }, { label: '密码', value: 'password', type: 'text', placeholder: '请输入密码', required: true }, { label: '性别', value: 'gender', type: 'select', placeholder: '请选择性别', options: [ { label: '男', value: 'male' }, { label: '女', value: 'female' } ], required: true }, { label: '生日', value: 'birthday', type: 'date', placeholder: '请选择生日', required: true } ] } }, methods: { handleSubmit(form) { // 处理表单提交逻辑 } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超人不会飞~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值