Skip to main content
Svelte基础
介绍
响应
属性Props
逻辑表达式
事件
绑定
Classes和样式
动作Actions
Transitions
Advanced Svelte
Advanced reactivity
Reusing content
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
Forms
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Hooks
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion

小主,不得不说至此我们都在跟内部状态打交道,也就是说仅限于组件内部自己玩。但上了战场,在实际的项目面前,这是不够的,我们往往需要把数据从组件传递到子组件。因此我们需要知道怎么定义 属性(properties) 简写为props。在Svelte中,我们可以使用$props符文(rune)获取组件的属性。上手吧,小主。先修改下Nested.svelte

So far, we’ve dealt exclusively with internal state — that is to say, the values are only accessible within a given component. 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>

Edit this page on GitHub

1
2
3
4
5
6
<script>
	import Nested from './Nested.svelte';
</script>
 
<Nested answer={42} />