shenrongliang
2025-04-07 e7fbf453476e31d7d9cf15b64acb6052fca6d78d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!-- 标签选项组件 -->
<template>
  <div class="flex flex-wrap gap-[8px]">
    <!-- 遍历标签数组,渲染每个标签 -->
    <span
      v-for="tag in props.tags"
      :key="tag.value"
      class="tag mb-2 border-[2px] border-solid border-[#DDDFE3] px-2 leading-6 text-[12px] bg-[#DDDFE3] rounded-[4px] cursor-pointer"
      :class="modelValue === tag.value && '!border-[#846af7] text-[#846af7]'"
      @click="emits('update:modelValue', tag.value)"
    >
      {{ tag.label }}
    </span>
  </div>
</template>
 
<script setup lang="ts">
// 定义组件的 props
const props = withDefaults(
  defineProps<{
    tags: { label: string; value: string }[] // 标签数组,包含标签文本和值
    modelValue: string // 当前选中的标签值
    [k: string]: any // 允许其他任意属性
  }>(),
  {
    tags: () => [] // 标签数组默认为空数组
  }
)
 
// 定义组件的 emits
const emits = defineEmits<{
  (e: 'update:modelValue', value: string): void // 更新选中标签值的事件
}>()
</script>
 
<!-- 组件样式 -->
<style scoped></style>