Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TyCoding opened this issue Oct 7, 2023 · 2 comments

Comments

@TyCoding
Copy link

TyCoding commented Oct 7, 2023

父子组件代码如下

父组件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
Copy link

yzlhdy commented Jan 6, 2024

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

@TheYon
Copy link

TheYon commented Jan 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants