//子组件声明props  (个人觉得这种既可以标注ts类型也可以设置默认值,推荐这个)
const props = defineProps({
  imgList: {
    default: [],
    type: Array<UploadUserFile>
  },
  limit: {//默认图片上传数量,默认是1
    type: Number,
    default: 1,
  },
  multiple: {//是否支持多选文件
    type: Boolean,
    default: false,
  },
  typeNumber: {
    type: Number,
    default: 18,
  },
})
//声明父组件传递下来的事件(ts版)(父组件在调用子组件的地方绑定自定义事件给子组件传递事件函数)
const emit = defineEmits<{
  (e: 'change', id: number): void
  (e: 'update', value: string): void
}>()
//触发
const click = () => {
  emit('change', 1)  
  emit('update', 'abc')
}
// 非ts版 的props和emit
const props = defineProps({
  foo: String
})

const emit = defineEmits(['change', 'delete'])
//ts版 声明props
// 1.接收父组件传值
const props = defineProps<{
  foo: string
  bar?: number
}>()

// 2.想要设置默认值,通过 withDefaults 编译器宏
interface Props {
  msg?: string
  labels?: string[]
}

const props = withDefaults(defineProps<Props>(), {
  msg: 'hello',
  labels: () => ['one', 'two']
})
Logo

前往低代码交流专区

更多推荐