Closed TyCoding closed 5 months ago
父子组件代码如下
父组件index.vue:
<template> <div> <n-button type="primary" @click="edit">打开Modal嵌套Form例子</n-button> <Edit ref="editRef" @ww="handleWW" /> </div> </template> <script lang="ts" setup> import { ref } from 'vue'; import Edit from './edit.vue'; const modalRef: any = ref(null); const editRef: any = ref(null); function edit() { editRef.value.show(); } function handleWW() { console.log('www'); } </script>
子组件edit.vue
<script setup lang="ts"> import { basicModal, useModal } from '@/components/Modal'; const [modalRegister, { openModal, closeModal, setSubLoading }] = useModal({ title: '新增预约', }); const emit = defineEmits(['ww']); async function okModal() {} function show() { console.log('show'); openModal(); } defineExpose({ show }); </script> <template> <basicModal @register="modalRegister" ref="modalRef" class="basicModal" @on-ok="okModal"> <template #default> 111 </template> </basicModal> </template>
直接刷新页面,就是报错runtime-core.esm-bundler.js:41 [Vue warn]: Component emitted event "register" but it is neither declared in the emits option nor as an "onRegister" prop.
问题在于子组件edit.vue中使用了 defineEmits,如果不使用则不会报错
解决了吗?不知道为啥添加了(e: 'register', value: any): void 这个就可以了
暂时没找到好的方案,我是通过 defineEmits['register',...] 解决这个问题的
父子组件代码如下
父组件index.vue:
子组件edit.vue
直接刷新页面,就是报错runtime-core.esm-bundler.js:41 [Vue warn]: Component emitted event "register" but it is neither declared in the emits option nor as an "onRegister" prop.
问题在于子组件edit.vue中使用了 defineEmits,如果不使用则不会报错