Vue3根據(jù)動(dòng)態(tài)字段綁定v-model的操作代碼
因業(yè)務(wù)需要用到低代碼,v-model綁定的是隨機(jī)生成的
<template>
<input type="text" v-model="form[key]">
<button @click="submit">提交</button>
</template>
<script setup>
import { ref } from "vue"
const form = ref({})
const key = ref("input-123456")
const submit = () => {
console.log(form.value)
}
</script>

VUE動(dòng)態(tài)綁定v-model變量(ui框架iview)
需求
最近在學(xué)習(xí)vue技術(shù),開(kāi)發(fā)表格的時(shí)候,想把表格做成組件,那查詢條件就需要?jiǎng)討B(tài)生成,這就遇到一個(gè)問(wèn)題,vue怎么動(dòng)態(tài)給v-model變量值。
UI框架使用的是iview
頁(yè)面渲染(子組件)
<div class="ivu-card-body">
<Form ref="formInline" v-model="formInline" inline>
<Row type="flex" justify="end" align="middle">
<Col
:xs="24"
:sm="24"
:md="6"
:lg="12"
:xl="8"
class="form-item-col"
v-for="(formItem, formIndex) in formArray"
:key="formItem.id"
:value="formIndex"
>
<FormItem :label="formItem.label + ':'">
<Input
v-if="formItem.componentType == 'input'"
:type="formItem.type"
v-model="formInline[formItem.model]"
:placeholder="formItem.placeholder"
>
</Input>
</FormItem>
</Col>
<Col
:xs="24"
:sm="24"
:md="6"
:lg="12"
:xl="8"
class="form-item-col ivu-btn-right"
>
<FormItem>
<Button
type="primary"
class="ivu-seach-btn"
@click="SeachHanler"
>搜索</Button>
</FormItem>
</Col>
</Row>
</Form>
</div>
子組件js
export default {
name: "TableSearch",
props: {
formArray: {
type: [Object, Array],
default: () => {}, //這邊是箭頭函數(shù)
},
},
data() {
var dataInit = {
formInline: {}, //查詢條件form
};
return dataInit;
},
watch: {
// 處理循環(huán)的數(shù)據(jù)
formArray() {
this.formArray.map((item) => {
if (item && item.model && item.modelValue) {
this.$set(this.formInline, item.model, item.modelValue);
}
});
},
},
methods: {
//搜索
SeachHanler() {
this.$emit("on-search", this.formInline);
}
},
mounted() {},
created() {},
};
父組件使用
data() {
return {
formArray: [
{
componentType: "input",
type: "text",
model: "UserName",
modelValue: "用戶1",
placeholder: "請(qǐng)輸入用戶名1111",
label: "用戶名111",
id: "1",
},
{
componentType: "input",
type: "text",
model: "Phone",
modelValue: "11111",
placeholder: "請(qǐng)輸入電話",
label: "電話",
id: "2",
}
]
};
},
遇到的問(wèn)題以及解決
1.問(wèn)題: 怎么讓變量值綁定到v-model上
解決: v-model="formInline[formItem.model]"
其中formInline是在form組件上定義的一個(gè)對(duì)象
2.問(wèn)題:變量值綁定上去了,怎么去讓數(shù)據(jù)顯示到子組件的“data”中
解決: 通過(guò)watch監(jiān)聽(tīng) formArray的值變化,然后使用下面的代碼進(jìn)行反顯,不然會(huì)導(dǎo)致v-model綁定的變量無(wú)法修改。this.$set(this.formInline, item.model, item.modelValue);
到此這篇關(guān)于Vue3根據(jù)動(dòng)態(tài)字段綁定v-model的文章就介紹到這了,更多相關(guān)Vue3動(dòng)態(tài)綁定v-model內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+springboot批量刪除功能實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue+springboot批量刪除功能,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
vue-resource?獲取本地json數(shù)據(jù)404問(wèn)題的解決
這篇文章主要介紹了vue-resource?獲取本地json數(shù)據(jù)404問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue 3 + Element Plus樹(shù)形表格全選多選及子節(jié)點(diǎn)勾選的問(wèn)題解決方
在本文中,我們解決了Vue 3和Element Plus樹(shù)形表格中的全選、多選、子節(jié)點(diǎn)勾選和父節(jié)點(diǎn)勾選等常見(jiàn)問(wèn)題,通過(guò)逐步實(shí)現(xiàn)這些功能,您可以構(gòu)建功能強(qiáng)大且用戶友好的樹(shù)形表格組件,以滿足各種數(shù)據(jù)展示需求,對(duì)Vue 3 Element Plus樹(shù)形表格相關(guān)知識(shí)感興趣的朋友一起看看吧2023-12-12
Vue.js實(shí)現(xiàn)網(wǎng)格列表布局轉(zhuǎn)換方法
下面小編就為大家?guī)?lái)一篇Vue.js實(shí)現(xiàn)網(wǎng)格列表布局轉(zhuǎn)換方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Jenkins自動(dòng)化部署Vue項(xiàng)目的方法實(shí)現(xiàn)
本文主要介紹了Jenkins自動(dòng)化部署Vue項(xiàng)目的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue 計(jì)算屬性和偵聽(tīng)器的使用小結(jié)
這篇文章主要介紹了vue 計(jì)算屬性和偵聽(tīng)器的使用小結(jié),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01
vue draggable resizable 實(shí)現(xiàn)可拖拽縮放的組件功能
這篇文章主要介紹了vue draggable resizable 實(shí)現(xiàn)可拖拽縮放的組件功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
vuex之this.$store.dispatch()與this.$store.commit()的區(qū)別及說(shuō)明
這篇文章主要介紹了vuex之this.$store.dispatch()與this.$store.commit()的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

