到目前为止,我们只处理了内部状态 - 也就是说,这些值只能在给定的组件内访问。
¥So far, we’ve dealt exclusively with internal state — that is to say, the values are only accessible within a given component.
在任何实际应用中,你都需要将数据从一个组件传递到其子组件。为此,我们需要声明属性,通常缩写为 ‘props’。在 Svelte 中,我们使用 $props
符文来实现这一点。编辑 Nested.svelte
组件:
¥In any real application, you’ll need to pass data from one component down to its children. To do that, we need to declare properties, generally shortened to ‘props’. In Svelte, we do that with the $props
rune. Edit the Nested.svelte
component:
Nested
<script>
let { answer } = $props();
</script>
<script lang="ts">
let { answer } = $props();
</script>
1
2
3
4
5
6
<script>
import Nested from './Nested.svelte';
</script>
<Nested answer={42} />