[Vue3] 关于vue3+ts中使用props进行 interface 类型限制设置默认值报错问题(props: Readonly<Props>))

[Vue3] 关于vue3+ts中使用props进行 interface 类型限制设置默认值报错问题(props: Readonly<Props>))_第1张图片
在使用 withDefaults 时,为自定义类声明默认值时报错。

报错信息:

Type '{}' is not assignable to type '(props: Readonly) => object'. Type '{}' provides no match for the signature '(props: Readonly): object'.

报错时的代码:

//props
interface Props {
  mydata:object,
}
const props = withDefaults(defineProps<Props>(), {
  mydata:{}
});

解决后的代码:

//props
interface Props {
  mydata: object;
}
const props = withDefaults(defineProps<Props>(), {
  mydata: () => {
    return {};
  },
});

我的示例:

[Vue3] 关于vue3+ts中使用props进行 interface 类型限制设置默认值报错问题(props: Readonly<Props>))_第2张图片

vue官方代码地址:https://cn.vuejs.org/guide/typescript/composition-api.html#typing-component-props

你可能感兴趣的:(Vue,前端,vue.js,javascript)