Vue 3 DefineProps 与 Types 和 ComponentObjectPropsOptions(如默认值或验证器)

2024-04-09

从设置方法中,使用defineProps我可以用

const props = defineProps<{tabs: Tab[]}> = ()

这让我可以拥有类型Tab[] on props.tabs

但是,如果我想指定ComponentObjectPropsOptions,我相信语法是

const props = defineProps = ({
  type: Array, //can not use Tab[] here
  required: true,
  validator: ...
})

但使用这种语法我失去了我的类型props.tabs :(


您需要使用PropType用于类型转换数组的实用程序。

https://vuejs.org/api/utility-types.html#proptype-t https://vuejs.org/api/utility-types.html#proptype-t

const props = defineProps({
  tabs: {
    type: Array as PropType<Tab[]>,
    required: true,
    validator: ...
  },
})

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue 3 DefineProps 与 Types 和 ComponentObjectPropsOptions(如默认值或验证器) 的相关文章

随机推荐