// 使用 withDefaults 定义组件的默认 props
<script setup lang="ts">
import { defineProps } from 'vue'
interface Props {
msg?: string
count?: number
}
// 使用 withDefaults 设置默认值
const props = withDefaults(defineProps<Props>(), {
msg: 'Hello World',
count: 0
})
</script>
<template>
<p>{{ msg }}</p>
<p>Count: {{ count }}</p>
</template>
<style scoped>
p {
font-size: 1.2em;
}
</style>
withDefaults
是 Vue 3 Composition API 中的一个函数,用于为组件的 props
设置默认值。Props
来描述 msg
和 count
这两个属性。withDefaults
函数为 msg
和 count
设置了默认值:msg
默认为 'Hello World'
,count
默认为 0
。props
,并且它们会根据父组件传递的值或默认值进行渲染。上一篇:vue点击子元素不触发父元素
下一篇:vue webpack配置
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站