vue3中標簽form插件的使用教程詳解
想寫一個系統(tǒng),對八字進行標注,比如格局,有些八字就有很多格局,于是就想著使用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>
到此這篇關(guān)于vue3中標簽form插件的使用教程詳解的文章就介紹到這了,更多相關(guān)vue3 form內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element-ui中實現(xiàn)tree子節(jié)點部分選中時父節(jié)點也選中
這篇文章主要介紹了element-ui中實現(xiàn)tree子節(jié)點部分選中時父節(jié)點也選中的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue3實現(xiàn)HTML內(nèi)容預(yù)覽功能
這篇文章主要為大家詳細介紹了如何使用Vue3實現(xiàn)HTML內(nèi)容預(yù)覽功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習一下2025-03-03Element?plus中el-input框回車觸發(fā)頁面刷新問題以及解決辦法
在el-form表單組件中el-input組件輸入內(nèi)容后按下Enter鍵刷新了整個頁面,下面這篇文章主要給大家介紹了關(guān)于Element?plus中el-input框回車觸發(fā)頁面刷新問題以及解決辦法,需要的朋友可以參考下2024-03-03Vue利用computed解決單項數(shù)據(jù)流的問題
Vue是一個非常流行和強大的前端框架,它讓我們可以用簡潔和優(yōu)雅的方式來構(gòu)建用戶界面,但是,Vue也有一些需要注意和掌握的細節(jié)和技巧,今天我們來分享一個Vue中非常經(jīng)典的問題,也是一個非常實用的技巧,Vue利用computed解決單項數(shù)據(jù)流,需要的朋友可以參考下2023-08-08vue 2 實現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
有時候我們需要對一個組件綁定自定義 v-model,以更方便地實現(xiàn)雙向數(shù)據(jù),例如自定義表單輸入控件,這篇文章主要介紹了vue 2 實現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法,需要的朋友可以參考下2024-07-07vue+css如何實現(xiàn)圓環(huán)漸變儀表盤
這篇文章主要介紹了vue+css如何實現(xiàn)圓環(huán)漸變儀表盤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08