泛型类型的赋默认值
当使用基于类型的声明式,就不能给props声明默认值了,此时将通过withDefaults编译宏解决:
export interface Props {
msg?: string
labels?: string[]
}
const props = withDefaults(defineProps<Props>(), {
msg: 'hello',
labels: () => ['one', 'two']
})
// 此时不能赋默认值
const props = defineProps<{
name: string,
id: number
}>()
// 使用props
props.name
非泛型类型赋默认值
const props = defineProps({
flag:{
required: false, // 是否必传
type: String, // 类型
default: 'table' // 默认值
},
name:{
required: true,
type: String,
}
});
// 使用
props.name