将整个应用放在一个组件中是不切实际的。相反,我们可以从其他文件导入组件并将它们包含在我们的标记中。
¥It would be impractical to put your entire app in a single component. Instead, we can import components from other files and include them in our markup.
在 App.svelte 顶部添加一个 <script> 标签,导入 Nested.svelte...
¥Add a <script> tag to the top of App.svelte that imports Nested.svelte...
App
<script>
import Nested from './Nested.svelte';
</script><script lang="ts">
import Nested from './Nested.svelte';
</script>...并包含 <Nested /> 组件:
¥...and include a <Nested /> component:
App
<p>This is a paragraph.</p>
<Nested />请注意,即使 Nested.svelte 具有 <p> 元素,App.svelte 中的样式也不会泄漏。
¥Notice that even though Nested.svelte has a <p> element, the styles from App.svelte don’t leak in.
组件名称大写,以区别于 HTML 元素。
1
2
3
4
5
6
7
8
9
10
<p>This is a paragraph.</p>
<style>
p {color: goldenrod;
font-family: 'Comic Sans MS', cursive;
font-size: 2em;
}
</style>