纸片
v-chip
组件用于传递小段信息。close
属性会让纸片变得可交互。此组件由 v-chip-group 用于高级选项。
用例
纸片有以下变量: Default, closeable, filter, outlined, pill。 默认的 v-chip
插槽文本的旁边也接受头像和图标。
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
示例
下面是一些简单到复杂的例子。
Material Design 调色板中的任何颜色都可用于更改纸片颜色。
<template>
<div class="text-center">
<v-chip
class="ma-2"
>
Default
</v-chip>
<v-chip
class="ma-2"
color="primary"
>
Primary
</v-chip>
<v-chip
class="ma-2"
color="secondary"
>
Secondary
</v-chip>
<v-chip
class="ma-2"
color="red"
text-color="white"
>
Red Chip
</v-chip>
<v-chip
class="ma-2"
color="green"
text-color="white"
>
Green Chip
</v-chip>
</div>
</template>
图标
纸片可以使用文本或“ Material Icons 字体库中可用的任何图标。
<template>
<div class="text-center">
<v-chip
class="ma-2"
color="indigo"
text-color="white"
>
<v-avatar left>
<v-icon>mdi-account-circle</v-icon>
</v-avatar>
Ranee
</v-chip>
<v-chip
class="ma-2"
color="orange"
text-color="white"
>
Premium
<v-icon right>mdi-star</v-icon>
</v-chip>
<v-chip
class="ma-2"
color="primary"
text-color="white"
>
1 Year
<v-icon right>mdi-cake-variant</v-icon>
</v-chip>
<v-chip
class="ma-2"
color="green"
text-color="white"
>
<v-avatar
left
class="green darken-4"
>
1
</v-avatar>
Years
</v-chip>
<v-chip
class="ma-2"
close
color="teal"
text-color="white"
@click:close="close"
>
<v-avatar left>
<v-icon>mdi-checkbox-marked-circle</v-icon>
</v-avatar>
Confirmed
</v-chip>
<v-chip
class="ma-2"
close
color="teal"
text-color="white"
close-icon="mdi-delete"
@click:close="close"
>
<v-avatar left>
<v-icon>mdi-checkbox-marked-circle</v-icon>
</v-avatar>
Confirmed
</v-chip>
</div>
</template>
<script>
export default {
methods: {
close () {
alert('Chip close clicked')
},
},
}
</script>
轮廓
轮廓纸片从当前文本颜色继承其边框颜色。
<template>
<div class="text-center">
<v-chip
class="ma-2"
color="success"
outlined
>
<v-icon left>mdi-server-plus</v-icon>
Server Status
</v-chip>
<v-chip
class="ma-2"
color="primary"
outlined
pill
>
User Account
<v-icon right>mdi-account-outline</v-icon>
</v-chip>
<v-chip
class="ma-2"
color="deep-purple accent-4"
outlined
>
<v-icon left>mdi-wrench</v-icon>
Update Settings
</v-chip>
<v-chip
class="ma-2"
close
color="indigo darken-3"
outlined
>
<v-icon left>mdi-fire</v-icon>
New Posts Available
</v-chip>
</div>
</template>
标签
标签纸片使用 v-card
border-radius。
<template>
<div class="text-center">
<v-chip
class="ma-2"
label
>
Label
</v-chip>
<v-chip
class="ma-2"
color="pink"
label
text-color="white"
>
<v-icon left>mdi-label</v-icon>
Tags
</v-chip>
<v-chip
class="ma-2"
color="primary"
label
>
<v-icon left>mdi-account-circle-outline</v-icon>
John Leider
</v-chip>
<v-chip
class="ma-2"
close
color="cyan"
label
text-color="white"
>
<v-icon left>mdi-twitter</v-icon>
New Tweets
</v-chip>
</div>
</template>
尺寸
v-chip
组件可以拥有不同的大小,从 x-small
到 x-large
。
<template>
<div class="text-center">
<v-chip
class="ma-2"
x-small
>
x-small chip
</v-chip>
<v-chip
class="ma-2"
small
>
small chip
</v-chip>
<v-chip
class="ma-2"
>
Default
</v-chip>
<v-chip
class="ma-2"
large
>
large chip
</v-chip>
<v-chip
class="ma-2"
x-large
>
x-large chip
</v-chip>
</div>
</template>
可拖动
draggable
v-chip
组件可以被鼠标拖放。
<template>
<v-row justify="center" align="center">
<v-chip draggable>Default</v-chip>
</v-row>
</template>
过滤器
v-chip
组件有 filter
选项,如果纸片处于活动状态,那么就会在该纸片内向您显示一个额外的图标。可以使用 filter-icon
自定义。
<template>
<v-row justify="space-around" align="center">
<v-chip
class="ma-2"
:input-value="active"
filter
>
I'm v-chip
</v-chip>
<v-chip
class="ma-2"
:input-value="active"
filter
filter-icon="mdi-plus"
>
I'm v-chip
</v-chip>
<v-chip
class="ma-2"
:input-value="active"
filter
filter-icon="mdi-minus"
>
I'm v-chip
</v-chip>
<v-switch v-model="active" label="Active"></v-switch>
</v-row>
</template>
<script>
export default {
data: () => ({
active: false,
}),
}
</script>
没有涟漪
如果 ripple
属性设置为 false
,则 v-chip
将不会渲染涟漪。
<template>
<v-row justify="center" align="center">
<v-chip :ripple="false">Default</v-chip>
</v-row>
</template>
可关闭
可关闭的纸片可以通过 v-model 进行控制。如果您想知道纸片何时关闭,也可以监听 click:close
事件。
<template>
<div class="text-center">
<v-btn
v-if="!chip1 && !chip2 && !chip3 && !chip4"
close
color="primary"
dark
@click="chip1 = true, chip2 = true, chip3 = true, chip4= true"
>
Reset Chips
</v-btn>
<v-chip
v-if="chip1"
class="ma-2"
close
@click:close="chip1 = false"
>
Closable
</v-chip>
<v-chip
v-if="chip2"
class="ma-2"
close
color="red"
text-color="white"
@click:close="chip2 = false"
>
Remove
</v-chip>
<v-chip
v-if="chip3"
class="ma-2"
close
color="green"
outlined
@click:close="chip3 = false"
>
Success
</v-chip>
<v-chip
v-if="chip4"
class="ma-2"
close
color="orange"
label
outlined
@click:close="chip4 = false"
>
Complete
</v-chip>
</div>
</template>
<script>
export default {
data () {
return {
chip1: true,
chip2: true,
chip3: true,
chip4: true,
}
},
}
</script>
操作纸片
纸片可以被操作。如果提供了click 事件,芯片将变成交互式的,并且可以调用方法。
<template>
<v-card
class="mx-auto"
max-width="450"
>
<v-img
src="https://cdn.vuetifyjs.com/images/cards/house.jpg"
:aspect-ratio="16/9"
>
</v-img>
<v-card-title>
<div class="display-1 mb-2">Welcome Home...</div>
<div class="title font-weight-regular grey--text">Monday, 12:30 PM, Mostly Sunny</div>
</v-card-title>
<v-row
class="px-4 grey--text"
align="center"
>
<v-avatar
size="24"
class="mr-4"
>
<v-img src="https://cdn.vuetifyjs.com/images/weather/part-cloud-48px.png" contain></v-img>
</v-avatar>
<span>81° / 62°</span>
</v-row>
<v-divider class="mt-6 mx-4"></v-divider>
<v-card-text>
<v-chip
class="mr-2"
@click="lights"
>
<v-icon left>mdi-brightness-5</v-icon>
Turn on Lights
</v-chip>
<v-chip
class="mr-2"
@click="alarm"
>
<v-icon left>mdi-alarm-check</v-icon>
Set alarm
</v-chip>
<v-chip @click="blinds">
<v-icon left>mdi-blinds</v-icon>
Close blinds
</v-chip>
</v-card-text>
</v-card>
</template>
<script>
export default {
methods: {
alarm () {
alert('Turning on alarm...')
},
blinds () {
alert('Toggling Blinds...')
},
lights () {
alert('Toggling lights...')
},
},
}
</script>
选中
选择框可以使用纸片显示所选数据。请尝试在下面添加您自己的标签。
<template>
<v-combobox
v-model="chips"
:items="items"
chips
clearable
label="Your favorite hobbies"
multiple
prepend-icon="filter_list"
solo
>
<template v-slot:selection="{ attrs, item, select, selected }">
<v-chip
v-bind="attrs"
:input-value="selected"
close
@click="select"
@click:close="remove(item)"
>
<strong>{{ item }}</strong>
<span>(interest)</span>
</v-chip>
</template>
</v-combobox>
</template>
<script>
export default {
data () {
return {
chips: ['Programming', 'Playing video games', 'Watching movies', 'Sleeping'],
items: ['Streaming', 'Eating'],
}
},
methods: {
remove (item) {
this.chips.splice(this.chips.indexOf(item), 1)
this.chips = [...this.chips]
},
},
}
</script>
自定义列表
在本例中,我们选择使用自定义列表来替代 v-autocomplete。这允许我们始终显示可用的选项,同时仍然提供相同的搜索和选择功能。
<template>
<v-card
class="mx-auto"
max-width="500"
>
<v-toolbar
flat
color="transparent"
>
<v-app-bar-nav-icon></v-app-bar-nav-icon>
<v-toolbar-title>Photo Info</v-toolbar-title>
<v-spacer></v-spacer>
<v-btn
icon
@click="$refs.search.focus()"
>
<v-icon>mdi-magnify</v-icon>
</v-btn>
</v-toolbar>
<v-container class="py-0">
<v-row
align="center"
justify="start"
>
<v-col
v-for="(selection, i) in selections"
:key="selection.text"
class="shrink"
>
<v-chip
:disabled="loading"
close
@click:close="selected.splice(i, 1)"
>
<v-icon
left
v-text="selection.icon"
></v-icon>
{{ selection.text }}
</v-chip>
</v-col>
<v-col v-if="!allSelected" cols="12">
<v-text-field
ref="search"
v-model="search"
full-width
hide-details
label="Search"
single-line
></v-text-field>
</v-col>
</v-row>
</v-container>
<v-divider v-if="!allSelected"></v-divider>
<v-list>
<template v-for="item in categories">
<v-list-item
v-if="!selected.includes(item)"
:key="item.text"
:disabled="loading"
@click="selected.push(item)"
>
<v-list-item-avatar>
<v-icon
:disabled="loading"
v-text="item.icon"
></v-icon>
</v-list-item-avatar>
<v-list-item-title v-text="item.text"></v-list-item-title>
</v-list-item>
</template>
</v-list>
<v-divider></v-divider>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn
:disabled="!selected.length"
:loading="loading"
color="purple"
text
@click="next"
>Next</v-btn>
</v-card-actions>
</v-card>
</template>
<script>
export default {
data: () => ({
items: [
{
text: 'Nature',
icon: 'mdi-nature',
},
{
text: 'Nightlife',
icon: 'mdi-glass-wine',
},
{
text: 'November',
icon: 'mdi-calendar-range',
},
{
text: 'Portland',
icon: 'mdi-map-marker',
},
{
text: 'Biking',
icon: 'mdi-bike',
},
],
loading: false,
search: '',
selected: [],
}),
computed: {
allSelected () {
return this.selected.length === this.items.length
},
categories () {
const search = this.search.toLowerCase()
if (!search) return this.items
return this.items.filter(item => {
const text = item.text.toLowerCase()
return text.indexOf(search) > -1
})
},
selections () {
const selections = []
for (const selection of this.selected) {
selections.push(selection)
}
return selections
},
},
watch: {
selected () {
this.search = ''
},
},
methods: {
next () {
this.loading = true
setTimeout(() => {
this.search = ''
this.selected = []
this.loading = false
}, 2000)
},
},
}
</script>
其他过滤器
纸片非常适合为特定任务提供辅助操作。在本例中,我们搜索项目列表并收集信息子集以显示可用的关键字。
<template>
<v-card
class="mx-auto mt-12"
max-width="450"
>
<v-system-bar></v-system-bar>
<v-toolbar
flat
color="transparent"
>
<v-btn icon>
<v-icon>mdi-arrow-left</v-icon>
</v-btn>
<v-text-field
v-model="search"
append-icon="mdi-magnify"
label="Search News"
single-line
></v-text-field>
</v-toolbar>
<v-card-text class="py-0">
<v-chip
v-for="(keyword, i) in keywords"
:key="i"
class="mr-2"
>
{{ keyword }}
</v-chip>
</v-card-text>
<v-list three-line>
<v-list-item
v-for="(item, i) in searching"
:key="i"
ripple
@click="() => {}"
>
<v-img
:src="item.image"
class="mr-4"
max-width="64"
min-width="64"
></v-img>
<v-list-item-content>
<span
class="text-uppercase font-weight-regular caption"
v-text="item.category"
></span>
<div v-text="item.title"></div>
</v-list-item-content>
</v-list-item>
</v-list>
</v-card>
</template>
<script>
export default {
data: () => ({
items: [
{
image: 'https://cdn-images-1.medium.com/max/1024/1*9C9hLji68wV373tk8okLYA.jpeg',
title: 'TBI’s 5 Best: SF Mocktails to Finish Dry January Strong',
category: 'Travel',
keyword: 'Drinks',
},
{
image: 'https://cdn-images-1.medium.com/max/1024/1*BBNtYUieAqHoXKjiJ2mMjQ.png',
title: 'PWAs on iOS 12.2 beta: the good, the bad, and the “not sure yet if good”',
category: 'Technology',
keyword: 'Phones',
},
{
image: 'https://cdn-images-1.medium.com/max/1024/1*rTEtei1UEmNqbq6evRsExw.jpeg',
title: 'How to Get Media Mentions for Your Business',
category: 'Media',
keyword: 'Social',
},
{
image: 'https://cdn-images-1.medium.com/max/1024/1*FD2nkJewVeQnGf0ommQfrw.jpeg',
title: 'The Pitfalls Of Outsourcing Self-Awareness To Artificial Intelligence',
category: 'Technology',
keyword: 'Military',
},
{
image: 'https://cdn-images-1.medium.com/max/1024/1*eogFpsVgNzXQLCVgFzT_-A.jpeg',
title: 'Degrees of Freedom and Sudoko',
category: 'Travel',
keyword: 'Social',
},
],
search: '',
}),
computed: {
keywords () {
if (!this.search) return []
const keywords = []
for (const search of this.searching) {
keywords.push(search.keyword)
}
return keywords
},
searching () {
if (!this.search) return this.items
const search = this.search.toLowerCase()
return this.items.filter(item => {
const text = item.title.toLowerCase()
return text.indexOf(search) > -1
})
},
},
}
</script>
可展开
纸片可以与 v-menu
结合使用,以启用纸片的一组特定动作。
<template>
<v-card
max-width="400"
class="mx-auto"
>
<v-system-bar></v-system-bar>
<v-row
class="px-6 py-3"
align="center"
>
<span class="mr-4">To</span>
<v-menu
v-model="menu"
bottom
right
transition="scale-transition"
origin="top left"
>
<template v-slot:activator="{ on }">
<v-chip
pill
v-on="on"
>
<v-avatar left>
<v-img src="https://cdn.vuetifyjs.com/images/john.png"></v-img>
</v-avatar>
John Leider
</v-chip>
</template>
<v-card width="300">
<v-list dark>
<v-list-item>
<v-list-item-avatar>
<v-img src="https://cdn.vuetifyjs.com/images/john.png"></v-img>
</v-list-item-avatar>
<v-list-item-content>
<v-list-item-title>John Leider</v-list-item-title>
<v-list-item-subtitle>john@vuetifyjs.com</v-list-item-subtitle>
</v-list-item-content>
<v-list-item-action>
<v-btn
icon
@click="menu = false"
>
<v-icon>mdi-close-circle</v-icon>
</v-btn>
</v-list-item-action>
</v-list-item>
</v-list>
<v-list>
<v-list-item @click="() => {}">
<v-list-item-action>
<v-icon>mdi-briefcase</v-icon>
</v-list-item-action>
<v-list-item-subtitle>john@gmail.com</v-list-item-subtitle>
</v-list-item>
</v-list>
</v-card>
</v-menu>
</v-row>
<v-divider></v-divider>
<v-text-field
full-width
value="Re: Vacation Request"
label="Subject"
single-line
></v-text-field>
<v-textarea
full-width
single-line
label="Message"
></v-textarea>
</v-card>
</template>
<script>
export default {
data: () => ({
menu: false,
}),
}
</script>