美文网首页
[Vue warn]: Invalid prop: type c

[Vue warn]: Invalid prop: type c

作者: holidayPenguin | 来源:发表于2023-11-07 14:24 被阅读0次

今天在重写ant design vue button 组件的时候,拦截click事件,出现的问题

问题代码

<template>
  <AButton @click="clickHandle" v-bind="props">
    <slot></slot>
  </AButton>
</template>
<script setup lang="ts">
import type { ButtonProps } from 'ant-design-vue'

const props = withDefaults(defineProps<ButtonProps & {
  /**
   * 鼠标点击延迟时间,默认500
   */
  delayTime?: number
}>(), {
  delayTime: 500,
})
</script>

原因是v-bind直接绑定了所有属性,需要剔除onClick,增加代码

const otherProps = computed(() => Object.assign({}, props, { onClick: undefined }))

v-bind改成绑定otherProps即可

总结:
遇到这类问题主要是两个组件使用了相同属性或者事件名,透传的时候就会有问题,如果是属性可以通过inheritAttrs解决

defineOptions({
  inheritAttrs: true,
})

事件就需要剔除多余属性

相关文章

网友评论

      本文标题:[Vue warn]: Invalid prop: type c

      本文链接:https://www.haomeiwen.com/subject/tdlfwdtx.html