vue3.2之defineProps

目录

一、方式一(运行时声明)

二、类型声明

三、总结


一、方式一(运行时声明)

<script setup>
defineProps({
    title: {
        type: String,
        default: '这是一个标题',
    },
    list: {
        type: Array,
        default: [],
    },
})
</script>

二、类型声明

<script setup lang="ts">
interface List {
    id: number,
    content: string,
}
defineProps<{
    title: string,
    list: List[],
}>()
</script>
  • withDefaults的参数二是写默认值
<script setup lang="ts">
interface List {
    id: number,
    content: string,
}
withDefaults(defineProps<{
    title: string,
    list: List[],
}>(), {
    title: '我是一个默认值'
})
</script>

三、总结

defineProps是一个宏命令,不需要导入

猜你喜欢

转载自blog.csdn.net/qq_52421092/article/details/131022844