Vue+ElementUI 字符串数组标签化展示组件

一. 效果

数据:'"苹果","香蕉"'

可添加,编辑,删除。

二. 组件源码

html 复制代码
<template>
  <div>
    <div v-for="(item, index) in items"
         :key="index">
      <el-input
        v-if="inputVisible && editingIndex === index"
        ref="input"
        v-model="inputValue"
        size="mini"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      />
      <el-tag
        v-else
        closable
        disable-transitions
        @close="removeTag(index)"
        @click="editTag(index)"
      >
        {{ item }}
      </el-tag>
    </div>
    <el-input
      v-if="inputVisible && editingIndex === null"
      ref="input"
      v-model="inputValue"
      size="mini"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    />
    <el-button v-else size="small" @click="showInput">+ 添加</el-button>
  </div>
</template>

<script>
export default {
  name: "EditableTags",
  props: {
    // 接收一个 JSON 数组字符串作为初始值
    initialItems: {
      type: String,
      default: '[]'
    }
  },
  data() {
    return {
      // 解析初始值
      items: JSON.parse(this.initialItems),
      inputVisible: false,
      inputValue: '',
      editingIndex: null
    };
  },
  watch: {
    // 监听 initialItems 的变化
    initialItems: {
      handler(newItems) {
        this.items = JSON.parse(newItems);
      },
      immediate: true
    },
    // 监听 items 的变化,触发父组件的更新事件
    items: {
      handler(newItems) {
        this.$emit('update:items', JSON.stringify(newItems));
      },
      deep: true
    }
  },
  methods: {
    removeTag(index) {
      this.items.splice(index, 1);
    },
    editTag(index) {
      this.editingIndex = index;
      this.inputValue = this.items[index];
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.input[0].focus();
      });
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      if (this.inputValue) {
        if (this.editingIndex !== null) {
          this.items[this.editingIndex] = this.inputValue;
          // 由于watch监控不到数组元素值的变化, 所以手动通知
          this.$set(this.items, this.editingIndex, this.inputValue);
          this.editingIndex = null;
        } else {
          this.items.push(this.inputValue);
        }
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
};
</script>

<style scoped>
.el-tag {
  margin-right: 10px;
}
</style>

三. 使用组件

html 复制代码
<template>
  <editable-tags :initial-items='items' @update:items="newItems => items = newItems" />
</template>

<script>
import EditableTags from "editableTags";

export default {
  components: {
    EditableTags
  },
  data() {
    return {
      items: '["苹果","香蕉"]',
    }
  }
};
</script>
相关推荐
vipbic2 小时前
别再把“做个H5”挂嘴边了:这个词,官方压根就没有定义过
前端
ZC跨境爬虫4 小时前
跟着 MDN 学CSS day_39:(Flexbox 弹性盒子核心机制)
前端·css·ui·html·tensorflow
小陈同学呦4 小时前
前端如何处理订单状态导航的数据竞态问题
前端·javascript
喵个咪4 小时前
GoWind Toolkit 前端代码生成|Vue3(ElementPlus/Vben)、React(AntDesign)全自动一键生成教程
前端·vue.js·react.js
qq_2518364575 小时前
SpringBoot+Vue 共享电池柜管理系统 完整实现 前后端分离项目实战 完整代码
vue.js·spring boot·后端
摆烂大大王6 小时前
玩转 OpenClaw:用 TaskFlow + Heartbeat 打造自动化工作流
前端·人工智能·自动化
zhangxingchao6 小时前
AI 大模型核心六:量化、Workflow 与 Agent、多轮 RAG
前端·人工智能·后端
梦想的颜色6 小时前
TypeScript 完全指南(上):从零开始掌握类型系统
前端·typescript
之歆6 小时前
Day01_ES6+ 专业指南:从基础到实战的现代JavaScript开发(下)
前端·javascript·es6
lichenyang4537 小时前
鸿蒙 MVVM 实战:从 Demo 到工程化,聊聊登录、状态管理与埋点系统设计
前端