评分
评级组件是构建用户小部件时的专门但至关重要的部分。 通过评级收集用户反馈是一种简单的分析,可以为您的产品或应用程序提供大量反馈。
用例
v-rating
组件提供了一个用于收集用户反馈的简单接口。
template script
<template>
<div class="text-center">
<v-rating v-model="rating"></v-rating>
</div>
</template>
<script>
export default {
data: () => ({
rating: 3,
}),
}
</script>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<div class="text-center">
<v-text-field
v-model="fullIcon"
color="red darken-4"
label="Full icon"
></v-text-field>
<v-text-field
v-model="halfIcon"
:disabled="!halfIncrements"
color="red darken-4"
label="Half icon"
></v-text-field>
<v-text-field
v-model="emptyIcon"
color="red darken-4"
label="Empty icon"
></v-text-field>
<v-row>
<v-switch
v-model="halfIncrements"
class="ma-2"
color="red darken-4"
label="Half increments"
></v-switch>
<v-switch
v-model="hover"
class="ma-2"
color="red darken-4"
label="Hover"
></v-switch>
<v-switch
v-model="readonly"
class="ma-2"
color="red darken-4"
label="Readonly"
></v-switch>
<v-switch
v-model="dense"
class="ma-2"
color="red darken-4"
label="Dense"
></v-switch>
</v-row>
<v-slider
v-model="length"
color="red darken-4"
min="1"
max="15"
label="Custom length"
></v-slider>
<v-slider
v-model="rating"
color="blue darken-4"
min="0"
:max="length"
:step="halfIncrements ? 0.5 : 1"
label="Value"
></v-slider>
<v-slider
v-model="size"
color="green darken-4"
min="0"
max="100"
label="Size"
></v-slider>
<v-autocomplete
v-model="color"
:items="colors"
label="Color"
></v-autocomplete>
<v-autocomplete
v-model="bgColor"
:items="bgColors"
label="Background color"
></v-autocomplete>
<v-rating
v-model="rating"
:length="length"
:empty-icon="emptyIcon"
:full-icon="fullIcon"
:half-icon="halfIcon"
:half-increments="halfIncrements"
:hover="hover"
:readonly="readonly"
:size="size"
:dense="dense"
:color="color"
:background-color="bgColor"
></v-rating>
<div>
<span class="caption text-uppercase">model:</span>
<span class="font-weight-bold">
{{ rating }}
</span>
</div>
</div>
</template>
<script>
export default {
data: () => ({
emptyIcon: 'mdi-heart-outline',
fullIcon: 'mdi-heart',
halfIcon: 'mdi-heart-half-full',
halfIncrements: false,
hover: true,
length: 5,
rating: 2,
readonly: false,
size: 64,
dense: false,
color: 'red lighten-3',
colors: [
'primary',
'warning',
'green',
'red',
'blue',
'error',
'teal',
'red lighten-3',
],
bgColor: 'grey lighten-1',
bgColors: [
'grey lighten-2',
'warning lighten-1',
'green lighten-2',
'red lighten-2',
'grey',
'#eee',
'cyan lighten-2',
'grey lighten-1',
],
}),
}
</script>
示例
下面是一些简单到复杂的例子。
大小变量
使用 v-icon
中可用的相同大小调整类,或为您自己提供 size 属性。
template script
<template>
<div class="text-center">
<v-rating
v-model="rating"
background-color="purple lighten-3"
color="purple"
small
></v-rating>
<v-rating
v-model="rating"
background-color="pink lighten-3"
color="pink"
></v-rating>
<v-rating
v-model="rating"
background-color="orange lighten-3"
color="orange"
medium
></v-rating>
<v-rating
v-model="rating"
background-color="green lighten-3"
color="green"
large
></v-rating>
<v-rating
v-model="rating"
background-color="red lighten-3"
color="red"
x-large
></v-rating>
<v-rating
v-model="rating"
background-color="indigo lighten-3"
color="indigo"
size="64"
></v-rating>
</div>
</template>
<script>
export default {
data: () => ({
rating: 4,
}),
}
</script>
色彩
v-rating
组件可以根据需要进行着色,可以设置选定和未选定的颜色。
template script
<template>
<div class="text-center">
<v-rating
v-model="rating"
background-color="purple lighten-3"
color="purple"
></v-rating>
<v-rating
v-model="rating"
background-color="pink lighten-3"
color="pink"
></v-rating>
<v-rating
v-model="rating"
background-color="orange lighten-3"
color="orange"
></v-rating>
<v-rating
v-model="rating"
background-color="green lighten-3"
color="green"
></v-rating>
<v-rating
v-model="rating"
background-color="red lighten-3"
color="red"
></v-rating>
<v-rating
v-model="rating"
background-color="indigo lighten-3"
color="indigo"
></v-rating>
</div>
</template>
<script>
export default {
data: () => ({
rating: 4,
}),
}
</script>
自定义长度
有时应用程序会调用自定义实现。轻松改变长度或显示图标。
template script
<template>
<div class="text-center">
<v-slider
v-model="length"
color="red darken-4"
min="1"
max="15"
label="Custom length"
></v-slider>
<v-rating
v-model="rating"
:length="length"
color="red lighten-3"
background-color="grey lighten-1"
></v-rating>
<div>
<span class="caption text-uppercase">model:</span>
<span class="font-weight-bold">
{{ rating }}
</span>
</div>
</div>
</template>
<script>
export default {
data: () => ({
length: 5,
rating: 2,
}),
}
</script>
增量
评分可以包含 3 个已定义的图标:full-icon, half-icon(带有 half-increments 属性)和 empty-icon。
template script
<template>
<v-card
class="elevation-16 mx-auto"
width="300"
>
<v-card-title
class="headline"
primary-title
>
Rate Our Framework
</v-card-title>
<v-card-text>
If you enjoy using Vuetify, please take a few seconds to rate your experience with the framework. It really helps!
<div class="text-center mt-12">
<v-rating
v-model="rating"
color="yellow darken-3"
background-color="grey darken-1"
empty-icon="$ratingFull"
half-increments
hover
></v-rating>
</div>
</v-card-text>
<v-divider></v-divider>
<v-card-actions class="justify-space-between">
<v-btn text>No Thanks</v-btn>
<v-btn
color="primary"
text
>
Rate Now
</v-btn>
</v-card-actions>
</v-card>
</template>
<script>
export default {
data: () => ({
rating: 4.5,
}),
}
</script>
Light Vuetify Stickers
Stick them to your car, laptop or favorite mug. They are made from a durable vinyl with a laminate that is weather-proof against rain, sun, wind and snow, and even dishwasher safe.
ads by Vuetify
](https://store.vuetifyjs.com/product/vuetify-light-sticker?ref=vuetifyjs.com)
插槽
提供了插槽,使您在显示评分时有更大的自由度。
template script
<template>
<div class="text-center">
<v-rating v-model="rating">
<template v-slot:item="props">
<v-icon
:color="props.isFilled ? genColor(props.index) : 'grey lighten-1'"
large
@click="props.click"
>
{{ props.isFilled ? 'mdi-star-circle' : 'mdi-circle-outline' }}
</v-icon>
</template>
</v-rating>
</div>
</template>
<script>
export default {
data: () => ({
colors: ['green', 'purple', 'orange', 'indigo', 'red'],
rating: 4.5,
}),
methods: {
genColor (i) {
return this.colors[i]
},
},
}
</script>
卡片评分
评分组件与产品搭配得很好,使您可以收集和显示客户反馈。
template script
<template>
<v-card
class="mx-auto elevation-20"
color="purple"
dark
style="max-width: 400px;"
>
<v-row justify="space-between">
<v-col cols="8">
<v-card-title primary-title>
<div>
<div class="headline">Halycon Days</div>
<div>Ellie Goulding</div>
<div>(2013)</div>
</div>
</v-card-title>
</v-col>
<v-img
class="shrink ma-2"
contain
height="125px"
src="https://cdn.vuetifyjs.com/images/cards/halcyon.png"
style="flex-basis: 125px"
></v-img>
</v-row>
<v-divider dark></v-divider>
<v-card-actions class="pa-4">
Rate this album
<v-spacer></v-spacer>
<span class="grey--text text--lighten-2 caption mr-2">
({{ rating }})
</span>
<v-rating
v-model="rating"
background-color="white"
color="yellow accent-4"
dense
half-increments
hover
size="18"
></v-rating>
</v-card-actions>
</v-card>
</template>
<script>
export default {
data: () => ({
rating: 4.3,
}),
}
</script>
高级用法
v-rating
组件恰好适合现有组件。 构建具有丰富功能和精美设计的真正复杂的示例。
template script
<template>
<v-card
class="mx-auto overflow-hidden"
style="max-width: 600px;"
>
<v-row>
<v-col class="d-flex" cols="6">
<v-img
src="https://cdn.vuetifyjs.com/images/ratings/fortnite1.png"
></v-img>
</v-col>
<v-col cols="6">
<v-container
class="pa-0 pl-2"
style="margin: -4px 0"
>
<v-row>
<v-col class="d-flex" cols="7">
<v-img
src="https://cdn.vuetifyjs.com/images/ratings/fortnite2.png"
></v-img>
</v-col>
<v-col class="d-flex" cols="5">
<v-img
src="https://cdn.vuetifyjs.com/images/ratings/fortnite3.png"
></v-img>
</v-col>
<v-col class="d-flex" cols="5">
<v-img
src="https://cdn.vuetifyjs.com/images/ratings/fortnite4.png"
></v-img>
</v-col>
<v-col class="d-flex" cols="7">
<v-img
src="https://cdn.vuetifyjs.com/images/ratings/fortnite5.png"
></v-img>
</v-col>
</v-row>
</v-container>
</v-col>
</v-row>
<v-card-title class="align-start">
<div>
<span class="headline">FORTNITE</span>
<div class="grey--text font-weight-light">Video game</div>
</div>
<v-spacer></v-spacer>
<v-dialog
v-model="dialog"
width="400"
>
<template v-slot:activator="{ on }">
<v-icon v-on="on">
mdi-share-variant
</v-icon>
</template>
<v-card>
<v-card-title>
<span class="title font-weight-bold">Share</span>
<v-spacer></v-spacer>
<v-btn
class="mx-0"
icon
@click="dialog = false"
>
<v-icon>mdi-close-circle-outline</v-icon>
</v-btn>
</v-card-title>
<v-list>
<v-list-item @click="">
<v-list-item-action>
<v-icon color="indigo">mdi-facebook-box</v-icon>
</v-list-item-action>
<v-card-title>Facebook</v-card-title>
</v-list-item>
<v-list-item @click="">
<v-list-item-action>
<v-icon color="cyan">mdi-twitter-box</v-icon>
</v-list-item-action>
<v-card-title>Twitter</v-card-title>
</v-list-item>
<v-list-item @click="">
<v-list-item-action>
<v-icon>mdi-email</v-icon>
</v-list-item-action>
<v-card-title>Email</v-card-title>
</v-list-item>
</v-list>
<v-text-field
ref="link"
:label="copied ? 'Link copied' : 'Click to copy link'"
class="pa-4"
readonly
value="https://g.co/kgs/nkrK43"
@click="copy"
></v-text-field>
</v-card>
</v-dialog>
</v-card-title>
<v-divider></v-divider>
<v-card-actions>
<span class="pl-2 grey--text text--darken-2 font-weight-light caption">16,544 reviews</span>
<v-spacer></v-spacer>
<v-rating
v-model="rating"
length="10"
readonly
>
<template v-slot:item="props">
<v-icon
:color="props.isFilled ? 'purple darken-4' : ''"
v-text="`mdi-numeric-${props.index}-box`"
></v-icon>
</template>
</v-rating>
</v-card-actions>
<div class="pa-4 pt-0 caption">
<em>Portions of the materials used are trademarks and/or copyrighted works of Epic Games, Inc. All rights reserved by Epic. This material is not official and is not endorsed by Epic.</em>
</div>
</v-card>
</template>
<script>
export default {
data: () => ({
copied: false,
dialog: false,
rating: 10,
}),
methods: {
copy () {
const markup = this.$refs.link
markup.focus()
document.execCommand('selectAll', false, null)
this.copied = document.execCommand('copy')
},
},
}
</script>