1、关于子组件接收父组件的值–props:使用defineProps接收到以后,开袋即食。
2、关于子组件发送数据给父组件–emits:
子组件:
<template>
<div>
<p>About</p>
<button @click="emitBtnClick">发送数据</button>
</div>
</template>
<script setup>
const emits = defineEmits(["clickInfoName"])
function emitBtnClick () {
emits("clickInfoName", "我是行李(参数)2333")
}
</script>
父组件:
<template>
<div class="app">
<p>App</p>
<button @click="currentPage = Home">home</button>
<button @click="currentPage = About">about</button>
<!-- 父组件监听 -->
<component :is="currentPage" @clickInfoName="test"></component>
</div>
</template>
<script setup>
import About from './views/About.vue'
import Home from './views/Home.vue'
let currentPage = shallowRef(Home)
function test (payload) {
console.log("监听到子组件发生了点击");
console.log("我是行李(参数):", payload);
}
</script>