前端工作小结77-新的封装组件
<!-- 可以动态新增的 tag 列表 --> <template> <div> <el-tag v-for="(tag, index) in dynamicTags" :key="index" :closable="true" :disable-transitions="false" @close="handleClose(tag)" > {{ tag }} </el-tag> <el-input ref="saveTagInput" class="input-new-tag" v-if="inputVisible" v-model="inputValue" @keyup.enter.native="$event.target.blur()" @blur="handleInputConfirm" /> <el-button v-else class="button-new-tag" size="small" @click="showInput"> + 添加 </el-button> </div> </template> <script> export default { name: "EditableTag", props: { dynamicTags: { type: Array, require: true } }, model: { event: "change", prop: "dynamicTags" }, data() { return { inputVisible: false, inputValue: "" }; }, methods: { handleClose(tag) { this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1); }, showInput() { this.inputVisible = true; this.$nextTick(() => { // auto focus this.$refs.saveTagInput.$refs.input.focus(); }); }, handleInputConfirm() { if (this.inputValue) { this.dynamicTags.push(this.inputValue); } this.inputVisible = false; this.inputValue = ""; } } }; </script> <style> .el-tag { margin-right: 10px; } .button-new-tag { height: 32px; line-height: 30px; padding-top: 0; padding-bottom: 0; } .input-new-tag { width: 90px; vertical-align: bottom; } </style>
这个组件封装的还是秒呀
值通过父组件传入
绑定change方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!