写一个vue的星星评分组件
时间: 2023-05-15 07:06:44 浏览: 112
好的,我可以回答这个问题。以下是一个简单的Vue星星评分组件的代码示例:
```html
<template>
<div class="star-rating">
<span v-for="n in maxStars"
:class="{ 'star': true, 'filled': n <= currentStars }"
@click="setStars(n)">
★
</span>
</div>
</template>
<script>
export default {
name: 'StarRating',
props: {
maxStars: {
type: Number,
default: 5
},
initialStars: {
type: Number,
default: 0
}
},
data() {
return {
currentStars: this.initialStars
}
},
methods: {
setStars(stars) {
this.currentStars = stars
}
}
}
</script>
<style scoped>
.star-rating {
display: inline-block;
font-size: 2em;
color: #ccc;
}
.star {
cursor: pointer;
}
.filled {
color: gold;
}
</style>
```
这个组件可以接受两个属性:`maxStars`和`initialStars`。`maxStars`表示最大星星数,默认为5;`initialStars`表示初始星星数,默认为0。用户可以点击星星来设置评分,点击后会触发`setStars`方法,将当前星星数更新为用户选择的星星数。