当前位置:网站首页>v-model组件化编程应用
v-model组件化编程应用
2022-07-30 14:20:00 【shewlong】
使用v-model完全接管子组件的数据(单文件)
父组件
<template>
<children v-model="formData"/>
</template>
<script setup>
import children from './children.vue'
const formData = reactive({
object:{
name:'',
sex:''
}})
</script>
子组件
<template>
<input v-model="formData.name" placeholder="请输入姓名" @update:value="updateValue('name', $event)">
</input>
<input v-model="formData.sex" placeholder="请输入性别" @update:value="updateValue('sex', $event)">
</input>
<button @click="submit">
提交
</button>
</template>
<script setup>
// defineProps和 toRefs在全局进行引入
const props = defineProps({
formData: {
type: Object,
default: () => {
return {
};
},
},
});
const emit = defineEmits(['update:model-value']);
//将reactive的值转为普通对象使用
const {
formData } = toRefs(props);
//提交表单信息
const submit = ()=>{
}
//状态更新
const updateValue = (fieldName: string, value: any): void => {
emit('update:model-value', Object.assign(modelValue.value, {
[fieldName]: value }));
};
</script>
此时,在父组件中的 f o r m D a t a 对象是实时变化的,可以随时拿出来用,无需封装对象过来取值 此时,在父组件中的formData对象是实时变化的,可以随时拿出来用,无需封装对象过来取值 此时,在父组件中的formData对象是实时变化的,可以随时拿出来用,无需封装对象过来取值
边栏推荐
- pytorch与keras的相互转换(代码以LeNet-5为例)
- Teach you how to write an eye-catching software testing resume, if you don't receive an interview invitation, I will lose
- 时序数据库在船舶风险管理领域的应用
- [机缘参悟-53]:《素书》-3-修身养志[求人之志章第三]
- Redis6.0 source code learning (5) ziplist
- Skywalking入门
- The use of ccs software (app software that makes money reliably)
- The evolution of content products has three axes: traffic, technology, and product form
- [Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)
- 算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
猜你喜欢

MongoDB starts an error Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)

3年软件测试经验面试要求月薪22K,明显感觉他背了很多面试题...

Conversion between pytorch and keras (the code takes LeNet-5 as an example)

以unity3d为例解读:游戏数据加密

selenium4+pyetsst+allure+pom进行自动化测试框架的最新设计

OFDM Sixteen Lectures 3- OFDM Waveforms

5. DOM

SLF4J的使用

CS内网横向移动 模拟渗透实操 超详细

Meta首份元宇宙白皮书9大看点,瞄准80万亿美元市场
随机推荐
eclipse连接SQL server数据库「建议收藏」
Shell变量与赋值、变量运算、特殊变量、重定向与管渠
Machine learning difference in the competition and industry application
MIMO雷达波形设计
What is defect analysis?An article takes you to understand the necessary skills of test engineers
Some thoughts on String
cookie模拟登录「建议收藏」
Android jump to google app market
kubernate部署服务
我为何从开发人员转做测试,3年软件测试工程师,带你聊聊这其中的秘辛
机器学习在竞赛和工业界应用区别
时序数据库在船舶风险管理领域的应用
产品年度营销计划书
Meta首份元宇宙白皮书9大看点,瞄准80万亿美元市场
开始学习C语言了
使用bat脚本以json格式导出mongo数据库中指定表数据
六面蚂蚁金服,抗住面试官的狂轰乱炸,前来面试复盘
Hello, World
selenium4+pyetsst+allure+pom进行自动化测试框架的最新设计
吃透Chisel语言.29.Chisel进阶之通信状态机(一)——通信状态机:以闪光灯为例