vue3中标签form插件的使用教程详解
更新时间:2024年01月05日 09:54:52 作者:warrah
这篇文章主要为大家详细介绍了vue3中标签form插件的使用的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以了解下
想写一个系统,对八字进行标注,比如格局,有些八字就有很多格局,于是就想着使用el-tag但是,form表单中如何处理呢?

这个时候,就需要自己写一个,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>到此这篇关于vue3中标签form插件的使用教程详解的文章就介绍到这了,更多相关vue3 form内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
element-ui中实现tree子节点部分选中时父节点也选中
这篇文章主要介绍了element-ui中实现tree子节点部分选中时父节点也选中的方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-08-08
Element plus中el-input框回车触发页面刷新问题以及解决办法
在el-form表单组件中el-input组件输入内容后按下Enter键刷新了整个页面,下面这篇文章主要给大家介绍了关于Element plus中el-input框回车触发页面刷新问题以及解决办法,需要的朋友可以参考下2024-03-03
vue 2 实现自定义组件一到多个v-model双向数据绑定的方法(最新推荐)
有时候我们需要对一个组件绑定自定义 v-model,以更方便地实现双向数据,例如自定义表单输入控件,这篇文章主要介绍了vue 2 实现自定义组件一到多个v-model双向数据绑定的方法,需要的朋友可以参考下2024-07-07


最新评论