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

embedded/2025/3/22 14:12:57/

一. 效果

数据:‘[“苹果”,“香蕉”]’
效果
在这里插入图片描述

可添加,编辑,删除。

二. 组件源码

<template><div><div v-for="(item, index) in items":key="index"><el-inputv-if="inputVisible && editingIndex === index"ref="input"v-model="inputValue"size="mini"@keyup.enter.native="handleInputConfirm"@blur="handleInputConfirm"/><el-tagv-elseclosabledisable-transitions@close="removeTag(index)"@click="editTag(index)">{{ item }}</el-tag></div><el-inputv-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>

三. 使用组件

<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>

http://www.ppmy.cn/embedded/174709.html

相关文章

练习-班级活动(map存储键值对)

问题描述 小明的老师准备组织一次班级活动。班上一共有 n 名 (n 为偶数) 同学&#xff0c;老师想把所有的同学进行分组&#xff0c;每两名同学一组。为了公平&#xff0c;老师给每名同学随机分配了一个 n 以内的正整数作为 id&#xff0c;第 i 名同学的 id 为 ai​。 老师希望…

基于微信小程序的充电桩管理系统

一、开发背景 在开发充电汽车管理系统之前&#xff0c;深入的需求分析至关重要。我们要充分了解不同用户群体的需求&#xff0c;比如私家车主希望充电过程便捷、高效、安全&#xff0c;能够实时查看充电状态和费用明细&#xff1b;出租车、网约车司机则更注重充电速度和充电桩…

mac anaconda3遇到无法创建python2.7版本虚拟环境

在Mac M4电脑上安装了Anaconda3之后,想通过conda创建python2.7的时候遇到错误: conda create -n python27 python=2.7(base) yuxuandong@dongyuxuandeMacBook-Air-2 ~ % conda create -n python27 python=2.7 Channels:- defaults- https://repo.anaconda.com/pkgs/main-

1.Go - Hello World

1.安装Go依赖 https://go.dev/dl/ 根据操作系统选择适合的依赖&#xff0c;比如windows&#xff1a; 2.配置环境变量 右键此电脑 - 属性 - 环境变量 PS&#xff1a; GOROOT&#xff1a;Go依赖路径&#xff1b; GOPATH&#xff1a;Go项目路径&#xff1b; …

画出ConcurrentHashMap 1.8的put流程图,记住CAS和synchronized的结合

ConcurrentHashMap 1.8 put 操作流程 结构&#xff1a;数组 链表/红黑树。 并发控制&#xff1a;CAS&#xff08;无锁操作&#xff09; synchronized&#xff08;锁住桶头部节点&#xff09;。 改进&#xff1a;相比1.7的分段锁&#xff08;Segment&#xff09;&#xff0c;…

MyBatis 的一次缓存与二次缓存

在数据访问层的开发中&#xff0c;MyBatis 是一款极为流行的持久层框架&#xff0c;它有效地简化了数据库操作的复杂性。而 MyBatis 的缓存机制更是其一大亮点&#xff0c;能够显著提升数据查询的效率&#xff0c;减少数据库的访问次数&#xff0c;从而优化系统性能。本文将深入…

【蓝桥杯速成】| 3.数据结构

题目一&#xff1a;两数之和 问题描述 1. 两数之和 - 力扣&#xff08;LeetCode&#xff09; 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会…

Linux top 命令详解:从入门到高级用法

Linux top 命令详解&#xff1a;从入门到高级用法 在 Linux 系统中&#xff0c;top 是一个强大的实时监控工具&#xff0c;用于查看系统资源使用情况和进程状态。它可以帮助你快速了解 CPU、内存、负载等信息&#xff0c;是系统管理员和开发者的日常利器。本文将从基本用法开始…