vue3中標(biāo)簽form插件的使用教程詳解
想寫一個系統(tǒng),對八字進(jìn)行標(biāo)注,比如格局,有些八字就有很多格局,于是就想著使用el-tag但是,form表單中如何處理呢?

這個時候,就需要自己寫一個,modelValue是表單的默認(rèn)屬性
<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="標(biāo)簽" prop="tags">
<udf-tags v-model="form.tags"></udf-tags>
</el-form-item>到此這篇關(guān)于vue3中標(biāo)簽form插件的使用教程詳解的文章就介紹到這了,更多相關(guān)vue3 form內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp+vue3路由跳轉(zhuǎn)傳參的實現(xiàn)
本文主要介紹了uniapp+vue3路由跳轉(zhuǎn)傳參的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
關(guān)于vue的element-ui web端引入高德地圖并獲取經(jīng)緯度
這篇文章主要介紹了關(guān)于vue的element-ui web端引入高德地圖并獲取經(jīng)緯度,高德地圖首先要去申請key和密鑰,文中提供了部分實現(xiàn)代碼和解決思路,感興趣的朋友可以學(xué)習(xí)一下2023-04-04
vue使用smooth-signature實現(xiàn)移動端橫豎屏電子簽字
這篇文章主要為大家介紹了vue使用smooth-signature實現(xiàn)移動端橫豎屏電子簽字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
vue?eslint報錯error?"Component?name?"*****"
這篇文章主要給大家介紹了關(guān)于vue?eslint報錯error?“Component?name?“*****“?should?always?be?multi-word”的解決方法,文中通過圖文以及實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Vue的transition-group與Virtual Dom Diff算法的使用
這篇文章主要介紹了Vue的transition-group與Virtual Dom Diff算法的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

