1. 父传子defineProps
1.1 父组件代码:(传递数据)
<Child :num="n"></Child>
<script setup>
import {ref} from 'vue';
import Child from './views/Child.vue'
const n = ref(3);
</script>
1.2 子组件代码:(接收数据)
<script setup>
defineProps({
num: {
type: Number,
default: 6
}
});
<script>
2. 子传父defineEmits
2.1 子组件代码:(传递数据)
<script setup>
let emit = defineEmits(['sendMsg']);
let clickEvent = function(){
emit('sendMsg','这是子组件传给父组件的数据');
}
<script>
2.2 父组件代码:(接收数据)
<Child @sendMsg='receiveEvent'></Child>
<script setup>
let receiveEvent = function(event){
console.log(event);
}
<script>