Data-tag标签

<!--由type属性来选择tag的类型,也可以通过color属性来自定义背景色。-->
<el-tag>标签一</el-tag>
<el-tag type="success">标签二</el-tag>
<el-tag type="info">标签三</el-tag>
<el-tag type="warning">标签四</el-tag>
<el-tag type="danger">标签五</el-tag>
<!--设置closable属性可以定义一个标签是否可移除(出现x,但动态编辑标签需要通过点击标签关闭按钮后触发的 close 事件来实现)。默认的标签移除时会附带渐变动画,如果不想使用,可以设置disable-transitions属性,它接受一个Boolean,true 为关闭。
-->
<el-tag
    :key="tag"
    v-for="tag in dynamicTags"
    closable
    :disable-transitions="false"
    @close="handleClose(tag)">
    {{tag}}
</el-tag>
<el-input
    class="input-new-tag"
    v-if="inputVisible"
    v-model="inputValue"
    ref="saveTagInput"
    size="small"
    @keyup.enter.native="handleInputConfirm"
    @blur="handleInputConfirm"
>
</el-input>
<el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                dynamicTags: ['标签一', '标签二', '标签三'],
                inputVisible: false,
                inputValue: ''
            };
        },
        methods: {
            handleClose(tag) {
                this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
            },

            showInput() {
                this.inputVisible = true;
                this.$nextTick(_ => {
                    this.$refs.saveTagInput.$refs.input.focus();
                });
            },

            handleInputConfirm() {
                let inputValue = this.inputValue;
                if (inputValue) {
                    this.dynamicTags.push(inputValue);
                }
                this.inputVisible = false;
                this.inputValue = '';
            }
        }
    })
</script>

Attributes

参数 说明 类型 可选值 默认值
type 主题 string success/info/warning/danger
closable 是否可关闭 boolean false
disable-transitions 是否禁用渐变动画 boolean false
color 背景色 string
size 尺寸 string medium / small / mini

Events

事件名称 说明 回调参数
close 关闭 Tag 时触发的事件

猜你喜欢

转载自blog.csdn.net/hrbsfdxzhq01/article/details/85811068