vue3中标签form插件

想写一个系统,对八字进行标注,比如格局,有些八字就有很多格局,于是就想着使用el-tag但是,form表单中如何处理呢?
1
这个时候,就需要自己写一个,modelValue是表单的默认属性

<template>
    <div>
        <el-tag v-for="(item,index) in keywordTags" :key="index" closable @close="handleClose(tag)"
        size="small" class="mx-1">{
   {
   item}}
        </el-tag>
        <el-input v-if="inputVisible" ref="InputRef" v-model="inputValue" class="ml-1 w-20" size="small" @keyup.enter="handleInputConfirm" @blur="handleInputConfirm"></el-input>
        <el-button v-else class="button-new-tag ml-1" size="small" @click="showInput">
            + 新增
        </el-button>
    </div>
</template>
<script lang="ts" setup>
const inputVisible = ref(false)
import {
    nextTick,ref,watch,getCurrentInstance } from 'vue'
import type {
    FormInstance, FormRules,ElInput  } from 'element-plus'
const {
    proxy }: any = getCurrentInstance();
const emits = defineEmits(['update:modelValue'])
const props = defineProps<{
     
    modelValue:String,
   
}>()
const keywordTags = ref([])
const inputValue = ref('')
const InputRef = ref<InstanceType<typeof ElInput>>()
const showInput  = () =>{
   
    inputVisible.value = true
    nextTick(() => {
   
        InputRef.value!.input!.focus()
    })
}
const handleClose = (tag:String) => {
   
    keywordTags.value.splice(keywordTags.value.indexOf(tag),1)
}
const handleInputConfirm = () => {
   
  if (inputValue.value) {
   
    keywordTags.value.push(inputValue.value)
  }
  inputVisible.value = false
  inputValue.value = ''
}   
watch(()=>keywordTags,(newVal,oldVal)=>{
   
    if (!proxy.$_.isEmpty(newVal.value)){
   
        console.log(newVal.value.join(','))
        emits('update:modelValue',newVal.value.join(','))
    }
},{
   immediate:true,deep:true})
watch(()=>props.modelValue,(newVal,oldVal)=>{
   
    if (!proxy.$_.isEmpty(newVal)){
   
        keywordTags.value = newVal.split(',')
    }
},{
   immediate:true,deep:true})
</script>
<style lang="less" scoped>
.w-20{
   
    width: 50px;
}
.mx-1{
   
    margin-right: 10px;
}
</style>
</style>

使用的话参见,这样保存和编辑就很容易了。

<el-form-item label="标签" prop="tags">
    <udf-tags v-model="form.tags"></udf-tags>
</el-form-item>

相关推荐

  1. vue3 编写

    2024-01-05 12:54:02       15 阅读
  2. vue3+ts自定义

    2024-01-05 12:54:02       35 阅读
  3. 使用vue3编写一个

    2024-01-05 12:54:02       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-05 12:54:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-05 12:54:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-05 12:54:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-05 12:54:02       20 阅读

热门阅读

  1. AWS ECS、EC2、EKS 和 Fargate 之间的关系

    2024-01-05 12:54:02       52 阅读
  2. selenium元素单击不稳定解决方法

    2024-01-05 12:54:02       40 阅读
  3. 技术人员可以成功转型项目经理和PMO吗?

    2024-01-05 12:54:02       36 阅读
  4. 面试经典150题(62-64)

    2024-01-05 12:54:02       29 阅读
  5. 中间件:构建现代软件架构的桥梁

    2024-01-05 12:54:02       34 阅读
  6. merge发生冲突时 ☞ 撤销merge

    2024-01-05 12:54:02       43 阅读
  7. WiFi7: MLD寻址

    2024-01-05 12:54:02       43 阅读