当前位置:网站首页>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对象是实时变化的,可以随时拿出来用,无需封装对象过来取值
边栏推荐
- Web3创始人和建设者必备指南:如何构建适合的社区?
- OFDM Sixteen Lectures 3- OFDM Waveforms
- The main content of terrain analysis (the special effect level of the wandering earth)
- [Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)
- 开始学习C语言了
- ESP32 反复重启问题 Arduino屏蔽断电探测器
- Baijiahao cancels the function of posting documents on the interface: the weight of the plug-in chain is blocked
- (论文翻译]未配对Image-To-Image翻译使用Cycle-Consistent敌对的网络
- JUC常见的线程池源码学习 02 ( ThreadPoolExecutor 线程池 )
- LoRaWAN网关源码分析(V1.0.2)
猜你喜欢

Understand the Chisel language. 29. Chisel advanced communication state machine (1) - communication state machine: take the flash as an example

Still saying software testing doesn't have a midlife crisis?9 years of test engineers were eliminated

一文读懂网络效应对Web3的重要意义

Skywalking入门

The truth of the industry: I will only test those that have no future, and I panic...

内容产品进化三板斧:流量、技术、产品形态

这个编辑器居然号称快如闪电!

mongodb打破原则引入SQL,它到底想要干啥?

时序数据库在船舶风险管理领域的应用

浅析显卡市场的未来走向:现在可以抄底了吗?
随机推荐
Androd 跳转到google应用市场
Web消息推送之SSE
UPC2022 Summer Individual Training Game 19 (B, P)
The use of ccs software (app software that makes money reliably)
时间序列的数据分析(四):STL分解
Shell变量与赋值、变量运算、特殊变量、重定向与管渠
Skywalking入门
ROS 导航
从实例来看DAO:权力分散的伟大尝试
基于FPGA的DDS任意波形输出
开源工具推荐:高性能计算辅助工具MegPeak
为什么做软件测试一定要学自动化?谈谈我眼中自动化测试的价值
元宇宙邮局AI航天主题系列数字藏品 将于7月30日10:00点上线“元邮数藏”
Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review
ESP32 反复重启问题 Arduino屏蔽断电探测器
内容产品进化三板斧:流量、技术、产品形态
Eight years of testing experience, why was the leader criticized: the test documents you wrote are not as good as those of fresh graduates
百家号取消接口发文功能:插外链获权重被堵死
The truth of the industry: I will only test those that have no future, and I panic...
Recommended open source tools: MegPeak, a high-performance computing tool