uniapp中语法糖setup模式父子组件v-model实现双向绑定输入框
uni-app
2026-11-16 10:45:52
uniapp开发中父子组件的通过v-model实现数据数据双向绑定,子组件中input输入框输入内容,动态实时在父级进行展示。
文章基于语法糖setup模式给出父子组件v-model双向绑定完整代码详解。
一、创建子组件childmodel数据双向绑定
创建子组件childmodel.vue,input值的双向绑定操作。
<template>
<view class="">
<input
:value="inputValue"
@input="updateValue($event.target.value)"
placeholder="input输入框模式"
/>
</view>
</template>
<script setup>
// 默认的 v-model 接收 inputValue
defineProps({
inputValue: {
type: String,
default: ''
}
});
// 触发 update:inputValue 事件
const emit = defineEmits(['update:inputValue']);
const updateValue = (value) => {
emit('update:inputValue', value);
};
</script>二、父级双向绑定数据展示操作
在父级页面中,配置展示数据双向绑定操作。
<template>
<view class="">
<childmodel v-model:inputValue="inputValue"></childmodel>
父级展示v-model内容:{{inputValue}}
</view>
</template>
<script setup>
import { ref } from 'vue';
import childmodel from "./childmodel.vue"
const inputValue =ref('')
</script>三、代码说明
1、子组件defineProps获取传递的参数
2、子组件defineEmits暴露是事件
3、v-model:inputValue双向绑定自定义名称参数
六月初字帖坊小程序
你想要的字帖模板及工具,这里都有!
925篇文章
26人已阅读
六月初