可以通过监听输入框的输入事件,在输入内容后动态生成一个tag。

示例代码如下:

<template>
  <div>
    <el-input placeholder="请输入内容" v-model="inputValue" @keyup.enter="addTag"></el-input>
    <el-tag v-for="(tag, index) in tags" :key="index" closable @close="removeTag(index)">{{ tag }}</el-tag>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputValue: '',
      tags: []
    }
  },
  methods: {
    addTag() {
      if (this.inputValue) {
        this.tags.push(this.inputValue);
        this.inputValue = '';
      }
    },
    removeTag(index) {
      this.tags.splice(index, 1);
    }
  }
}
</script>

在上述示例中,当输入框输入内容后,按下回车键触发addTag方法,将输入的内容添加到tags数组中,并清空输入框。然后使用v-for指令动态渲染tag列表,同时设置closable属性使得tag可关闭,点击tag的关闭按钮触发removeTag方法,将对应的tag从tags数组中移除


原文地址: https://www.cveoy.top/t/topic/eov6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录