工做211:新的封裝組件 秒呀

<!-- 能夠動態新增的 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>

這個組件封裝的仍是秒呀ui

值經過父組件傳入this

綁定change方法code

相關文章
相關標籤/搜索