vue3.2 script setup 父子组件传值

父组件代码

复制代码
<!-- 父组件代码 -->
<template>
  <div id="nav">
    <AboutVue :value="parentValue" @add="onParentClick" />
    <br />
    <button @click.stop="onParentClick()">父组件按钮</button>
  </div>
</template>
<script setup>
/**
 * 引入必要的模块
 */
import { ref } from 'vue'
import AboutVue from './views/About.vue'
/**
 * 定义数据
 */
const parentValue = ref(0)

/**
 * 定义方法
 */
function onParentClick (childValue) {
  childValue ? (parentValue.value += childValue) : parentValue.value++
}
</script>
<style scoped>
#nav {
  text-align: center;
}
</style>
复制代码

子组件代码

复制代码
<!-- 子组件代码 -->
<template>
  <div class="about">
    <h1>{{ props.value }}</h1>
    <button @click="onChildClick">子组件按钮</button>
  </div>
</template>
<script setup>
/**
 * 引入必要的模块
 */
import { defineProps, defineEmits } from 'vue'
/**
 * 定义数据
 */
const props = defineProps({ value: { type: Number } })
const emit = defineEmits(['add'])

/**
 * 定义方法
 */
function onChildClick () {
  emit('add', 2)
}
</script>
复制代码

 

vue3.2 setup 语法糖中:子组件使用 defineProps接收props , 使用 defineEmits传递给参数给父组件

 


posted @   祁腾飞  阅读(2903)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示