纸片

v-chip 组件用于传递小段信息。close 属性会让纸片变得可交互。此组件由 v-chip-group 用于高级选项。

用例

纸片有以下变量: Default, closeable, filter, outlined, pill。 默认的 v-chip 插槽文本的旁边也接受头像和图标。

Chips(纸片) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

API

示例

下面是一些简单到复杂的例子。

Material Design 调色板中的任何颜色都可用于更改纸片颜色。

Chips(纸片) - 图3

  1. <template>
  2. <div class="text-center">
  3. <v-chip
  4. class="ma-2"
  5. >
  6. Default
  7. </v-chip>
  8. <v-chip
  9. class="ma-2"
  10. color="primary"
  11. >
  12. Primary
  13. </v-chip>
  14. <v-chip
  15. class="ma-2"
  16. color="secondary"
  17. >
  18. Secondary
  19. </v-chip>
  20. <v-chip
  21. class="ma-2"
  22. color="red"
  23. text-color="white"
  24. >
  25. Red Chip
  26. </v-chip>
  27. <v-chip
  28. class="ma-2"
  29. color="green"
  30. text-color="white"
  31. >
  32. Green Chip
  33. </v-chip>
  34. </div>
  35. </template>

图标

纸片可以使用文本或“ Material Icons 字体库中可用的任何图标。

Chips(纸片) - 图4

  1. <template>
  2. <div class="text-center">
  3. <v-chip
  4. class="ma-2"
  5. color="indigo"
  6. text-color="white"
  7. >
  8. <v-avatar left>
  9. <v-icon>mdi-account-circle</v-icon>
  10. </v-avatar>
  11. Ranee
  12. </v-chip>
  13. <v-chip
  14. class="ma-2"
  15. color="orange"
  16. text-color="white"
  17. >
  18. Premium
  19. <v-icon right>mdi-star</v-icon>
  20. </v-chip>
  21. <v-chip
  22. class="ma-2"
  23. color="primary"
  24. text-color="white"
  25. >
  26. 1 Year
  27. <v-icon right>mdi-cake-variant</v-icon>
  28. </v-chip>
  29. <v-chip
  30. class="ma-2"
  31. color="green"
  32. text-color="white"
  33. >
  34. <v-avatar
  35. left
  36. class="green darken-4"
  37. >
  38. 1
  39. </v-avatar>
  40. Years
  41. </v-chip>
  42. <v-chip
  43. class="ma-2"
  44. close
  45. color="teal"
  46. text-color="white"
  47. @click:close="close"
  48. >
  49. <v-avatar left>
  50. <v-icon>mdi-checkbox-marked-circle</v-icon>
  51. </v-avatar>
  52. Confirmed
  53. </v-chip>
  54. <v-chip
  55. class="ma-2"
  56. close
  57. color="teal"
  58. text-color="white"
  59. close-icon="mdi-delete"
  60. @click:close="close"
  61. >
  62. <v-avatar left>
  63. <v-icon>mdi-checkbox-marked-circle</v-icon>
  64. </v-avatar>
  65. Confirmed
  66. </v-chip>
  67. </div>
  68. </template>
  69. <script>
  70. export default {
  71. methods: {
  72. close () {
  73. alert('Chip close clicked')
  74. },
  75. },
  76. }
  77. </script>

轮廓

轮廓纸片从当前文本颜色继承其边框颜色。

Chips(纸片) - 图5

  1. <template>
  2. <div class="text-center">
  3. <v-chip
  4. class="ma-2"
  5. color="success"
  6. outlined
  7. >
  8. <v-icon left>mdi-server-plus</v-icon>
  9. Server Status
  10. </v-chip>
  11. <v-chip
  12. class="ma-2"
  13. color="primary"
  14. outlined
  15. pill
  16. >
  17. User Account
  18. <v-icon right>mdi-account-outline</v-icon>
  19. </v-chip>
  20. <v-chip
  21. class="ma-2"
  22. color="deep-purple accent-4"
  23. outlined
  24. >
  25. <v-icon left>mdi-wrench</v-icon>
  26. Update Settings
  27. </v-chip>
  28. <v-chip
  29. class="ma-2"
  30. close
  31. color="indigo darken-3"
  32. outlined
  33. >
  34. <v-icon left>mdi-fire</v-icon>
  35. New Posts Available
  36. </v-chip>
  37. </div>
  38. </template>

标签

标签纸片使用 v-card border-radius。

Chips(纸片) - 图6

  1. <template>
  2. <div class="text-center">
  3. <v-chip
  4. class="ma-2"
  5. label
  6. >
  7. Label
  8. </v-chip>
  9. <v-chip
  10. class="ma-2"
  11. color="pink"
  12. label
  13. text-color="white"
  14. >
  15. <v-icon left>mdi-label</v-icon>
  16. Tags
  17. </v-chip>
  18. <v-chip
  19. class="ma-2"
  20. color="primary"
  21. label
  22. >
  23. <v-icon left>mdi-account-circle-outline</v-icon>
  24. John Leider
  25. </v-chip>
  26. <v-chip
  27. class="ma-2"
  28. close
  29. color="cyan"
  30. label
  31. text-color="white"
  32. >
  33. <v-icon left>mdi-twitter</v-icon>
  34. New Tweets
  35. </v-chip>
  36. </div>
  37. </template>

尺寸

v-chip 组件可以拥有不同的大小,从 x-smallx-large

Chips(纸片) - 图7

  1. <template>
  2. <div class="text-center">
  3. <v-chip
  4. class="ma-2"
  5. x-small
  6. >
  7. x-small chip
  8. </v-chip>
  9. <v-chip
  10. class="ma-2"
  11. small
  12. >
  13. small chip
  14. </v-chip>
  15. <v-chip
  16. class="ma-2"
  17. >
  18. Default
  19. </v-chip>
  20. <v-chip
  21. class="ma-2"
  22. large
  23. >
  24. large chip
  25. </v-chip>
  26. <v-chip
  27. class="ma-2"
  28. x-large
  29. >
  30. x-large chip
  31. </v-chip>
  32. </div>
  33. </template>

可拖动

draggable v-chip 组件可以被鼠标拖放。

Chips(纸片) - 图8

  1. <template>
  2. <v-row justify="center" align="center">
  3. <v-chip draggable>Default</v-chip>
  4. </v-row>
  5. </template>

过滤器

v-chip 组件有 filter 选项,如果纸片处于活动状态,那么就会在该纸片内向您显示一个额外的图标。可以使用 filter-icon 自定义。

Chips(纸片) - 图9

  1. <template>
  2. <v-row justify="space-around" align="center">
  3. <v-chip
  4. class="ma-2"
  5. :input-value="active"
  6. filter
  7. >
  8. I'm v-chip
  9. </v-chip>
  10. <v-chip
  11. class="ma-2"
  12. :input-value="active"
  13. filter
  14. filter-icon="mdi-plus"
  15. >
  16. I'm v-chip
  17. </v-chip>
  18. <v-chip
  19. class="ma-2"
  20. :input-value="active"
  21. filter
  22. filter-icon="mdi-minus"
  23. >
  24. I'm v-chip
  25. </v-chip>
  26. <v-switch v-model="active" label="Active"></v-switch>
  27. </v-row>
  28. </template>
  29. <script>
  30. export default {
  31. data: () => ({
  32. active: false,
  33. }),
  34. }
  35. </script>

没有涟漪

如果 ripple 属性设置为 false,则 v-chip 将不会渲染涟漪。

Chips(纸片) - 图10

  1. <template>
  2. <v-row justify="center" align="center">
  3. <v-chip :ripple="false">Default</v-chip>
  4. </v-row>
  5. </template>

可关闭

可关闭的纸片可以通过 v-model 进行控制。如果您想知道纸片何时关闭,也可以监听 click:close 事件。

Chips(纸片) - 图11

  1. <template>
  2. <div class="text-center">
  3. <v-btn
  4. v-if="!chip1 && !chip2 && !chip3 && !chip4"
  5. close
  6. color="primary"
  7. dark
  8. @click="chip1 = true, chip2 = true, chip3 = true, chip4= true"
  9. >
  10. Reset Chips
  11. </v-btn>
  12. <v-chip
  13. v-if="chip1"
  14. class="ma-2"
  15. close
  16. @click:close="chip1 = false"
  17. >
  18. Closable
  19. </v-chip>
  20. <v-chip
  21. v-if="chip2"
  22. class="ma-2"
  23. close
  24. color="red"
  25. text-color="white"
  26. @click:close="chip2 = false"
  27. >
  28. Remove
  29. </v-chip>
  30. <v-chip
  31. v-if="chip3"
  32. class="ma-2"
  33. close
  34. color="green"
  35. outlined
  36. @click:close="chip3 = false"
  37. >
  38. Success
  39. </v-chip>
  40. <v-chip
  41. v-if="chip4"
  42. class="ma-2"
  43. close
  44. color="orange"
  45. label
  46. outlined
  47. @click:close="chip4 = false"
  48. >
  49. Complete
  50. </v-chip>
  51. </div>
  52. </template>
  53. <script>
  54. export default {
  55. data () {
  56. return {
  57. chip1: true,
  58. chip2: true,
  59. chip3: true,
  60. chip4: true,
  61. }
  62. },
  63. }
  64. </script>

操作纸片

纸片可以被操作。如果提供了click 事件,芯片将变成交互式的,并且可以调用方法。

Chips(纸片) - 图12

  1. <template>
  2. <v-card
  3. class="mx-auto"
  4. max-width="450"
  5. >
  6. <v-img
  7. src="https://cdn.vuetifyjs.com/images/cards/house.jpg"
  8. :aspect-ratio="16/9"
  9. >
  10. </v-img>
  11. <v-card-title>
  12. <div class="display-1 mb-2">Welcome Home...</div>
  13. <div class="title font-weight-regular grey--text">Monday, 12:30 PM, Mostly Sunny</div>
  14. </v-card-title>
  15. <v-row
  16. class="px-4 grey--text"
  17. align="center"
  18. >
  19. <v-avatar
  20. size="24"
  21. class="mr-4"
  22. >
  23. <v-img src="https://cdn.vuetifyjs.com/images/weather/part-cloud-48px.png" contain></v-img>
  24. </v-avatar>
  25. <span>81° / 62°</span>
  26. </v-row>
  27. <v-divider class="mt-6 mx-4"></v-divider>
  28. <v-card-text>
  29. <v-chip
  30. class="mr-2"
  31. @click="lights"
  32. >
  33. <v-icon left>mdi-brightness-5</v-icon>
  34. Turn on Lights
  35. </v-chip>
  36. <v-chip
  37. class="mr-2"
  38. @click="alarm"
  39. >
  40. <v-icon left>mdi-alarm-check</v-icon>
  41. Set alarm
  42. </v-chip>
  43. <v-chip @click="blinds">
  44. <v-icon left>mdi-blinds</v-icon>
  45. Close blinds
  46. </v-chip>
  47. </v-card-text>
  48. </v-card>
  49. </template>
  50. <script>
  51. export default {
  52. methods: {
  53. alarm () {
  54. alert('Turning on alarm...')
  55. },
  56. blinds () {
  57. alert('Toggling Blinds...')
  58. },
  59. lights () {
  60. alert('Toggling lights...')
  61. },
  62. },
  63. }
  64. </script>

选中

选择框可以使用纸片显示所选数据。请尝试在下面添加您自己的标签。

Chips(纸片) - 图13

  1. <template>
  2. <v-combobox
  3. v-model="chips"
  4. :items="items"
  5. chips
  6. clearable
  7. label="Your favorite hobbies"
  8. multiple
  9. prepend-icon="filter_list"
  10. solo
  11. >
  12. <template v-slot:selection="{ attrs, item, select, selected }">
  13. <v-chip
  14. v-bind="attrs"
  15. :input-value="selected"
  16. close
  17. @click="select"
  18. @click:close="remove(item)"
  19. >
  20. <strong>{{ item }}</strong>&nbsp;
  21. <span>(interest)</span>
  22. </v-chip>
  23. </template>
  24. </v-combobox>
  25. </template>
  26. <script>
  27. export default {
  28. data () {
  29. return {
  30. chips: ['Programming', 'Playing video games', 'Watching movies', 'Sleeping'],
  31. items: ['Streaming', 'Eating'],
  32. }
  33. },
  34. methods: {
  35. remove (item) {
  36. this.chips.splice(this.chips.indexOf(item), 1)
  37. this.chips = [...this.chips]
  38. },
  39. },
  40. }
  41. </script>

自定义列表

在本例中,我们选择使用自定义列表来替代 v-autocomplete。这允许我们始终显示可用的选项,同时仍然提供相同的搜索和选择功能。

Chips(纸片) - 图14

  1. <template>
  2. <v-card
  3. class="mx-auto"
  4. max-width="500"
  5. >
  6. <v-toolbar
  7. flat
  8. color="transparent"
  9. >
  10. <v-app-bar-nav-icon></v-app-bar-nav-icon>
  11. <v-toolbar-title>Photo Info</v-toolbar-title>
  12. <v-spacer></v-spacer>
  13. <v-btn
  14. icon
  15. @click="$refs.search.focus()"
  16. >
  17. <v-icon>mdi-magnify</v-icon>
  18. </v-btn>
  19. </v-toolbar>
  20. <v-container class="py-0">
  21. <v-row
  22. align="center"
  23. justify="start"
  24. >
  25. <v-col
  26. v-for="(selection, i) in selections"
  27. :key="selection.text"
  28. class="shrink"
  29. >
  30. <v-chip
  31. :disabled="loading"
  32. close
  33. @click:close="selected.splice(i, 1)"
  34. >
  35. <v-icon
  36. left
  37. v-text="selection.icon"
  38. ></v-icon>
  39. {{ selection.text }}
  40. </v-chip>
  41. </v-col>
  42. <v-col v-if="!allSelected" cols="12">
  43. <v-text-field
  44. ref="search"
  45. v-model="search"
  46. full-width
  47. hide-details
  48. label="Search"
  49. single-line
  50. ></v-text-field>
  51. </v-col>
  52. </v-row>
  53. </v-container>
  54. <v-divider v-if="!allSelected"></v-divider>
  55. <v-list>
  56. <template v-for="item in categories">
  57. <v-list-item
  58. v-if="!selected.includes(item)"
  59. :key="item.text"
  60. :disabled="loading"
  61. @click="selected.push(item)"
  62. >
  63. <v-list-item-avatar>
  64. <v-icon
  65. :disabled="loading"
  66. v-text="item.icon"
  67. ></v-icon>
  68. </v-list-item-avatar>
  69. <v-list-item-title v-text="item.text"></v-list-item-title>
  70. </v-list-item>
  71. </template>
  72. </v-list>
  73. <v-divider></v-divider>
  74. <v-card-actions>
  75. <v-spacer></v-spacer>
  76. <v-btn
  77. :disabled="!selected.length"
  78. :loading="loading"
  79. color="purple"
  80. text
  81. @click="next"
  82. >Next</v-btn>
  83. </v-card-actions>
  84. </v-card>
  85. </template>
  86. <script>
  87. export default {
  88. data: () => ({
  89. items: [
  90. {
  91. text: 'Nature',
  92. icon: 'mdi-nature',
  93. },
  94. {
  95. text: 'Nightlife',
  96. icon: 'mdi-glass-wine',
  97. },
  98. {
  99. text: 'November',
  100. icon: 'mdi-calendar-range',
  101. },
  102. {
  103. text: 'Portland',
  104. icon: 'mdi-map-marker',
  105. },
  106. {
  107. text: 'Biking',
  108. icon: 'mdi-bike',
  109. },
  110. ],
  111. loading: false,
  112. search: '',
  113. selected: [],
  114. }),
  115. computed: {
  116. allSelected () {
  117. return this.selected.length === this.items.length
  118. },
  119. categories () {
  120. const search = this.search.toLowerCase()
  121. if (!search) return this.items
  122. return this.items.filter(item => {
  123. const text = item.text.toLowerCase()
  124. return text.indexOf(search) > -1
  125. })
  126. },
  127. selections () {
  128. const selections = []
  129. for (const selection of this.selected) {
  130. selections.push(selection)
  131. }
  132. return selections
  133. },
  134. },
  135. watch: {
  136. selected () {
  137. this.search = ''
  138. },
  139. },
  140. methods: {
  141. next () {
  142. this.loading = true
  143. setTimeout(() => {
  144. this.search = ''
  145. this.selected = []
  146. this.loading = false
  147. }, 2000)
  148. },
  149. },
  150. }
  151. </script>

其他过滤器

纸片非常适合为特定任务提供辅助操作。在本例中,我们搜索项目列表并收集信息子集以显示可用的关键字。

Chips(纸片) - 图15

  1. <template>
  2. <v-card
  3. class="mx-auto mt-12"
  4. max-width="450"
  5. >
  6. <v-system-bar></v-system-bar>
  7. <v-toolbar
  8. flat
  9. color="transparent"
  10. >
  11. <v-btn icon>
  12. <v-icon>mdi-arrow-left</v-icon>
  13. </v-btn>
  14. <v-text-field
  15. v-model="search"
  16. append-icon="mdi-magnify"
  17. label="Search News"
  18. single-line
  19. ></v-text-field>
  20. </v-toolbar>
  21. <v-card-text class="py-0">
  22. <v-chip
  23. v-for="(keyword, i) in keywords"
  24. :key="i"
  25. class="mr-2"
  26. >
  27. {{ keyword }}
  28. </v-chip>
  29. </v-card-text>
  30. <v-list three-line>
  31. <v-list-item
  32. v-for="(item, i) in searching"
  33. :key="i"
  34. ripple
  35. @click="() => {}"
  36. >
  37. <v-img
  38. :src="item.image"
  39. class="mr-4"
  40. max-width="64"
  41. min-width="64"
  42. ></v-img>
  43. <v-list-item-content>
  44. <span
  45. class="text-uppercase font-weight-regular caption"
  46. v-text="item.category"
  47. ></span>
  48. <div v-text="item.title"></div>
  49. </v-list-item-content>
  50. </v-list-item>
  51. </v-list>
  52. </v-card>
  53. </template>
  54. <script>
  55. export default {
  56. data: () => ({
  57. items: [
  58. {
  59. image: 'https://cdn-images-1.medium.com/max/1024/1*9C9hLji68wV373tk8okLYA.jpeg',
  60. title: 'TBI’s 5 Best: SF Mocktails to Finish Dry January Strong',
  61. category: 'Travel',
  62. keyword: 'Drinks',
  63. },
  64. {
  65. image: 'https://cdn-images-1.medium.com/max/1024/1*BBNtYUieAqHoXKjiJ2mMjQ.png',
  66. title: 'PWAs on iOS 12.2 beta: the good, the bad, and the “not sure yet if good”',
  67. category: 'Technology',
  68. keyword: 'Phones',
  69. },
  70. {
  71. image: 'https://cdn-images-1.medium.com/max/1024/1*rTEtei1UEmNqbq6evRsExw.jpeg',
  72. title: 'How to Get Media Mentions for Your Business',
  73. category: 'Media',
  74. keyword: 'Social',
  75. },
  76. {
  77. image: 'https://cdn-images-1.medium.com/max/1024/1*FD2nkJewVeQnGf0ommQfrw.jpeg',
  78. title: 'The Pitfalls Of Outsourcing Self-Awareness To Artificial Intelligence',
  79. category: 'Technology',
  80. keyword: 'Military',
  81. },
  82. {
  83. image: 'https://cdn-images-1.medium.com/max/1024/1*eogFpsVgNzXQLCVgFzT_-A.jpeg',
  84. title: 'Degrees of Freedom and Sudoko',
  85. category: 'Travel',
  86. keyword: 'Social',
  87. },
  88. ],
  89. search: '',
  90. }),
  91. computed: {
  92. keywords () {
  93. if (!this.search) return []
  94. const keywords = []
  95. for (const search of this.searching) {
  96. keywords.push(search.keyword)
  97. }
  98. return keywords
  99. },
  100. searching () {
  101. if (!this.search) return this.items
  102. const search = this.search.toLowerCase()
  103. return this.items.filter(item => {
  104. const text = item.title.toLowerCase()
  105. return text.indexOf(search) > -1
  106. })
  107. },
  108. },
  109. }
  110. </script>

可展开

纸片可以与 v-menu 结合使用,以启用纸片的一组特定动作。

Chips(纸片) - 图16

  1. <template>
  2. <v-card
  3. max-width="400"
  4. class="mx-auto"
  5. >
  6. <v-system-bar></v-system-bar>
  7. <v-row
  8. class="px-6 py-3"
  9. align="center"
  10. >
  11. <span class="mr-4">To</span>
  12. <v-menu
  13. v-model="menu"
  14. bottom
  15. right
  16. transition="scale-transition"
  17. origin="top left"
  18. >
  19. <template v-slot:activator="{ on }">
  20. <v-chip
  21. pill
  22. v-on="on"
  23. >
  24. <v-avatar left>
  25. <v-img src="https://cdn.vuetifyjs.com/images/john.png"></v-img>
  26. </v-avatar>
  27. John Leider
  28. </v-chip>
  29. </template>
  30. <v-card width="300">
  31. <v-list dark>
  32. <v-list-item>
  33. <v-list-item-avatar>
  34. <v-img src="https://cdn.vuetifyjs.com/images/john.png"></v-img>
  35. </v-list-item-avatar>
  36. <v-list-item-content>
  37. <v-list-item-title>John Leider</v-list-item-title>
  38. <v-list-item-subtitle>john@vuetifyjs.com</v-list-item-subtitle>
  39. </v-list-item-content>
  40. <v-list-item-action>
  41. <v-btn
  42. icon
  43. @click="menu = false"
  44. >
  45. <v-icon>mdi-close-circle</v-icon>
  46. </v-btn>
  47. </v-list-item-action>
  48. </v-list-item>
  49. </v-list>
  50. <v-list>
  51. <v-list-item @click="() => {}">
  52. <v-list-item-action>
  53. <v-icon>mdi-briefcase</v-icon>
  54. </v-list-item-action>
  55. <v-list-item-subtitle>john@gmail.com</v-list-item-subtitle>
  56. </v-list-item>
  57. </v-list>
  58. </v-card>
  59. </v-menu>
  60. </v-row>
  61. <v-divider></v-divider>
  62. <v-text-field
  63. full-width
  64. value="Re: Vacation Request"
  65. label="Subject"
  66. single-line
  67. ></v-text-field>
  68. <v-textarea
  69. full-width
  70. single-line
  71. label="Message"
  72. ></v-textarea>
  73. </v-card>
  74. </template>
  75. <script>
  76. export default {
  77. data: () => ({
  78. menu: false,
  79. }),
  80. }
  81. </script>