我们可以在 Nested.svelte 中轻松地为 props 指定默认值:
Nested
<script>
let { answer = 'a mystery' } = $props();
</script><script lang="ts">
let { answer = 'a mystery' } = $props();
</script>现在,如果我们添加第二个组件,但不指定 answer 属性,它将使用默认值:
App
<Nested answer={42}/>
<Nested />1
2
3
4
5
6
<script>
import Nested from './Nested.svelte';
</script>
<Nested answer={42} />