出现报错emit is not a function
我使用的是setup()函数,不是使用setup挂载的形式,然后一开始子组件写法如下,打印emit为空
const emit = defineEmits(['closeCart']);
console.log(emit);//输出为空
const close= () => {
emit('closeCart')
};
子组件修改后
export default defineComponent({
setup(props, {emit}) {
console.log(emit);//(event, ...args) => instance.emit(event, ...args)
const close = () =>{
emit('closeCart')
}
return{
close
}
}
})
父组件
<template>
<index v-if="!showCart" @closeCart = 'closeCart'/>
<template/>
<script setup>
import { ref } from 'vue';
const showCart = ref<boolean>(true)
//显示购物车
const onShowCart = () =>{
showCart.value = false
}
//关闭购物车
const closeCart = () =>{
showCart.value = !showCart.value
}
<script/>