当前位置:首页 > VUE

vue实现tag效果

2026-03-08 09:20:23VUE

Vue 实现 Tag 标签效果

在 Vue 中实现 Tag 标签效果可以通过多种方式完成,以下是一些常见的方法和实现步骤。

使用原生 HTML 和 CSS

通过简单的 HTML 结构和 CSS 样式,可以快速实现基础的 Tag 效果。

<template>
  <div class="tag-container">
    <span class="tag" v-for="tag in tags" :key="tag">{{ tag }}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tags: ['Vue', 'JavaScript', 'CSS', 'HTML']
    }
  }
}
</script>

<style>
.tag-container {
  display: flex;
  gap: 8px;
}

.tag {
  padding: 4px 8px;
  background-color: #e0e0e0;
  border-radius: 4px;
  font-size: 14px;
}
</style>

使用组件库

许多流行的 Vue 组件库(如 Element UI、Ant Design Vue)提供了现成的 Tag 组件。

以 Element UI 为例:

<template>
  <el-tag v-for="tag in tags" :key="tag" type="success">{{ tag }}</el-tag>
</template>

<script>
export default {
  data() {
    return {
      tags: ['Vue', 'JavaScript', 'CSS', 'HTML']
    }
  }
}
</script>

动态添加和删除 Tag

通过绑定事件和方法,可以实现动态添加和删除 Tag 的功能。

<template>
  <div>
    <input v-model="newTag" @keyup.enter="addTag" placeholder="输入标签后按回车" />
    <div class="tag-container">
      <span class="tag" v-for="(tag, index) in tags" :key="index">
        {{ tag }}
        <button @click="removeTag(index)">×</button>
      </span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tags: ['Vue', 'JavaScript'],
      newTag: ''
    }
  },
  methods: {
    addTag() {
      if (this.newTag.trim() && !this.tags.includes(this.newTag.trim())) {
        this.tags.push(this.newTag.trim());
        this.newTag = '';
      }
    },
    removeTag(index) {
      this.tags.splice(index, 1);
    }
  }
}
</script>

<style>
.tag-container {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.tag {
  padding: 4px 8px;
  background-color: #e0e0e0;
  border-radius: 4px;
  font-size: 14px;
}

button {
  background: none;
  border: none;
  cursor: pointer;
}
</style>

自定义 Tag 组件

如果需要更复杂的 Tag 功能,可以封装一个自定义的 Tag 组件。

<!-- Tag.vue -->
<template>
  <span class="custom-tag" :style="{ backgroundColor: color }">
    <slot></slot>
    <span class="close-btn" @click="$emit('close')">×</span>
  </span>
</template>

<script>
export default {
  props: {
    color: {
      type: String,
      default: '#e0e0e0'
    }
  }
}
</script>

<style>
.custom-tag {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
}

.close-btn {
  margin-left: 4px;
  cursor: pointer;
}
</style>

在父组件中使用:

vue实现tag效果

<template>
  <div>
    <Tag v-for="(tag, index) in tags" :key="index" @close="removeTag(index)">
      {{ tag }}
    </Tag>
  </div>
</template>

<script>
import Tag from './Tag.vue';

export default {
  components: { Tag },
  data() {
    return {
      tags: ['Vue', 'JavaScript', 'CSS', 'HTML']
    }
  },
  methods: {
    removeTag(index) {
      this.tags.splice(index, 1);
    }
  }
}
</script>

以上方法可以根据具体需求选择使用,灵活调整样式和功能。

标签: 效果vue
分享给朋友:

相关文章

vue实现图片搜索

vue实现图片搜索

Vue实现图片搜索功能 使用HTML5的File API获取图片 通过<input type="file">元素让用户选择图片文件,使用FileReader对象读取图片数据。 <t…

vue为啥能实现监听

vue为啥能实现监听

Vue 实现监听的原理 Vue 的监听能力主要基于其响应式系统,该系统通过数据劫持和依赖收集实现。当数据变化时,系统能自动触发视图更新或执行回调函数。 核心机制:Object.definePrope…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…

vue nexttick实现

vue nexttick实现

Vue 中 nextTick 的实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它的核心实现依赖于 JavaScript 的事件循环机制。 基本…

vue实现筛选

vue实现筛选

实现筛选功能的基本思路 在Vue中实现筛选功能通常需要结合数据绑定、计算属性和方法。筛选的核心逻辑是根据用户输入的条件过滤原始数据列表,并动态更新显示结果。 数据准备 定义一个数组存储原始数据,另一…