你知道defineProps有多少种写法吗?

335 阅读1分钟

前言

你知道defineProps有多少种写法吗?

你知道犹大有多少种用法吗?

前言: 出于这篇文章的初心是因为,在一个Vue3.X项目中发现了不同同事写的内容,对于defineProps的写法千奇百怪 所以出于好奇,来小小整理一下大概还有多少种千奇百怪的写法。(如有丢失,请评论区补充~)

1.非TS写法

const props = defineProps({
  data: {
    type: String,
    default: ''
  },
  arr: {
    type: Array,
    default: ()=>[]
  },
})

2.不需要默认值的TS写法

const  props = defineProps<{
    data: string,
    arr:number[]
}>()

3.需要默认值的TS写法

需要注意的是 withDefaults 只在 ts 中有用

const props = withDefaults(defineProps<{
  data: string,
  arr: number[] 
}>(), {
  data: '我是默认值',
  arr: () => [23]
})