前端工作小结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方法

 

posted @   前端导师歌谣  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示