long
2021-09-13 5f052d4e17b53d4fe07a87d53a9c112bff3dc852
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!-- 服务标签 -->
<template>
  <div class="serveLabel">
    <el-tag
      v-for="tag in labelList"
      :key="tag"
      closable
      :disable-transitions="false"
      @close="labelCloseHandle(tag)"
    >
      {{ tag }}
    </el-tag>
    <el-input
      v-if="labelInputVisible"
      ref="saveTagInput"
      v-model="labelValue"
      class="input-new-tag"
      size="small"
      maxlength="50"
      placeholder="输入标签,回车分隔"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    />
    <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 添加{{ text||'标签' }}</el-button>
  </div>
</template>
 
<script>
export default {
  name: 'ServeLabels',
  model: {
    prop: 'param_labelList', // 使用 v-model
    event: 'change' // 绑定change事件
  },
  props: [
    'param_labelList', // 标签
    'text'
  ],
  data() {
    return {
      // 标签输入
      labelValue: '',
      // 是否显示标签输入框
      labelInputVisible: false,
      // 标签数组
      labelList: []
    }
  },
  watch: {
    // 监听shopId,并触发change事件
    labelList(value) {
      this.$emit('change', value)
    },
    param_labelList(value) {
      this.labelList = value
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.labelList = this.param_labelList || []
    },
 
    // 删除标签
    labelCloseHandle(tag) {
      this.labelList.splice(this.labelList.indexOf(tag), 1)
    },
    // 显示输入框
    showInput() {
      this.labelInputVisible = true
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 输入标签完成
    handleInputConfirm() {
      const inputValue = this.labelValue
 
      if (this.labelList.indexOf(inputValue) > -1) {
        this.$messageError('请勿重复输入标签')
      } else {
        if (inputValue) {
          this.labelList.push(inputValue)
        }
      }
 
      // 隐藏输入框
      this.labelInputVisible = false
      // 清空输入内容
      this.labelValue = ''
    }
  }
}
</script>
 
<style>
.serveLabel .el-tag {
  margin-right: 10px;
  margin-bottom: 10px;
  font-size: 14px;
}
.button-new-tag {
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
  font-size: 14px !important;
}
.input-new-tag {
  width: 160px;
  vertical-align: bottom;
}
.input-new-tag .el-input__inner{
  font-size: 14px;
}
</style>