xiangpei
2025-04-10 69b975aad77dea4c6c10a21b278248ce091f54eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<template>
  <div>
    <div>
      <el-button v-if="!inputVisible" class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
    </div>
    <div>
      <el-input
        v-if="inputVisible"
        class="input-new-tag"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      >
      </el-input>
      <div class="tags-container">
        <el-tag
          v-for="(tag, index) in tags"
          :key="tag + index"
          closable
          @close="handleClose(index)">
          {{tag}}
        </el-tag>
      </div>
    </div>
 
  </div>
</template>
 
<script>
export default {
  props: {
    tagList: {
      required: true,
      type: Array
    }
  },
  watch: {
    tagList(newV, oldV) {
      this.tags = newV
    }
  },
  data() {
    return {
      tags: [],
      inputVisible: false,
      inputValue: ''
    };
  },
  methods: {
    setTags(list) {
      this.tags = list;
    },
    handleClose(index) {
      console.log("点了")
      this.tags.splice(index, 1);
      this.$emit("getTags", this.tags)
    },
 
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
 
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.tags.push(inputValue);
        this.$emit("getTags", this.tags)
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>
 
<style>
.el-tag + .el-tag {
  margin-left: 10px;
 
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
 
.tags-container {
  margin-top: 10px; /* 根据需要调整间距 */
}
</style>