jekip / naive-ui-admin

Naive Ui Admin 是一款基于 Vue3、Vite3 和 TypeScript 的先进中后台解决方案,集成了前沿的前端技术栈和典型业务模型。它拥有二次封装组件、动态菜单、权限校验、粒子化权限控制等核心功能,旨在帮助企业快速构建高质量的中后台项目。无论在新技术运用或业务实践层面,都能为您提供有力支持,并将持续更新,以满足您不断变化的需求
https://naive-ui-admin.vercel.app
MIT License
5.04k stars 935 forks source link

子组件中使用useModal当定义emit后报错Component emitted event "register" but it is neither declared in the emits option nor as an "onRegister" prop #263

Closed TyCoding closed 5 months ago

TyCoding commented 1 year 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,如果不使用则不会报错

yzlhdy commented 11 months ago

解决了吗?不知道为啥添加了(e: 'register', value: any): void 这个就可以了

TheYon commented 10 months ago

暂时没找到好的方案,我是通过 defineEmits['register',...] 解决这个问题的