uniapp中vue3语法糖父子组件计算属性实现数据双向绑定代码详解
uni-app
2026-11-16 10:43:30
uniapp开发多端项目中,计算属性是最为常见的使用操作。本篇文章就来介绍如何基于计算属性实现父子组件的数据双向绑定操作。
一、创建子组件childmodel
创建子组件childmodel.vue,input值的通过计算属性实现双向绑定操作。
<template>
<view class="">
<input v-model="inputValue" placeholder="使用计算属性" />
</view>
</template>
<script setup>
import { computed } from 'vue';
const emit = defineEmits(['update:inputValue']);
// 需要先声明 props
const props = defineProps(['inputValue']);
const inputValue = computed({
get: () => props.modelValue,
set: (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篇文章
28人已阅读
六月初