用defineModel实现vue子组件间传递数据

一、背景和意义

vue开发中,时常会涉及到子组件之间传递数据。经典的方法是使用propsemit,而vue 3.4+提供的defineModel方法可以用更少的代码实现这一功能。本文提供一个defineModel方法实现子组件间传递数据的示例。

二、经典方法实现子组件间传递数据

经典方法是使用propsemit,父组件代码如下:

html 复制代码
<template>
  <div>
    <h1>value in parent: {{transferValue}}</h1>
    <ChildA :transferValue="transferValue" @changeValue="handleChangeValue"/>
    <ChildB :transferValue="transferValue" />
  </div>
</template>
<script setup>
  import { ref } from "vue";
  const transferValue = ref("Hello");
  const handleChangeValue = newVal => {
    transferValue.value = newVal;
  };
</script>

子组件ChildA.vue的代码如下:

html 复制代码
<template>
  <div>
    <h1>value in ChildA: {{transferValue}}</h1>
    <button @click="changeValue">change value by childA</button>
  </div>
</template>
<script setup>
  defineProps({
    transferValue: ""
  });
  const emits = defineEmits(['changeValue']);
  const changeValue = () => {
    emits("changeValue", "hello2");
  };
</script>

子组件ChildB.vue的代码如下:

html 复制代码
<template>
  <div>
    <h1>value in ChildB: {{transferValue}}</h1>
    <h1>value length in ChildB: {{valueLength}}</h1>
  </div>
</template>
<script setup>
  import { computed } from "vue";
  const props = defineProps({
    transferValue: ""
  });
  const valueLength = computed(() => {
    return props.transferValue?.length || 0;
  });
</script>

运行效果如下:

点击子组件ChildAchange value by childA按钮之后,父组件和两个子组件的数据均发生了变化。

三、实现子组件间传递数据

使用defineModel需要3.4及以上的vue版本。父组件改为:

html 复制代码
<template>
  <div>
    <h1>value in parent: {{transferValue}}</h1>
    <ChildA v-model="transferValue"/>
    <ChildB v-model="transferValue" />
  </div>
</template>
<script setup>
  import { ref } from "vue"
  const transferValue = ref("Hello");
</script>

子组件ChildA改为:

html 复制代码
<template>
  <div>
    <h1>value in ChildA: {{transferValue}}</h1>
    <button @click="changeValue">change value by childA</button>
  </div>
</template>
<script setup>
  const transferValue = defineModel();
  const changeValue = () => {
    transferValue.value = "hello2";
  };
</script>

子组件ChildB改为:

html 复制代码
<template>
  <div>
    <h1>value in ChildB: {{transferValue}}</h1>
    <h1>value length in ChildB: {{valueLength}}</h1>
  </div>
</template>
<script setup>
  import { computed } from "vue";
  const transferValue = defineModel();
  const valueLength = computed(() => {
    return transferValue?.value?.length || 0;
  });
</script>

程序运行效果和前面经典方法的效果一样,但新的defineModel方法代码更少。

相关推荐
lbh2 小时前
当我开始像写代码一样和AI对话,一切都变了
前端·openai·ai编程
We་ct3 小时前
LeetCode 918. 环形子数组的最大和:两种解法详解
前端·数据结构·算法·leetcode·typescript·动态规划·取反
wefly20173 小时前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
C澒4 小时前
微前端容器标准化 —— 公共能力篇:通用打印
前端·架构
德育处主任Pro4 小时前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
木斯佳4 小时前
前端八股文面经大全:小红书前端一二面OC(下)·(2026-03-17)·面经深度解析
前端·vue3·proxy·八股·响应式
陈天伟教授4 小时前
人工智能应用- 预测新冠病毒传染性:04. 中国:强力措施遏制疫情
前端·人工智能·安全·xss·csrf
zayzy5 小时前
前端八股总结
开发语言·前端·javascript
今天减肥吗5 小时前
前端面试题
开发语言·前端·javascript
Rabbit_QL5 小时前
【前端UI行话】前端 UI 术语速查表
前端·ui·状态模式