|
1 | 1 | ---
|
2 |
| -title: Component fundamentals |
| 2 | +title: Public API of a component |
3 | 3 | ---
|
4 | 4 |
|
5 |
| -- script (module) / template / style (rough overview) |
6 |
| -- `$props` / `$state` (in the context of components) |
7 |
| - |
8 |
| -Components are the building blocks of Svelte applications. They are written into `.svelte` files, using a superset of HTML. |
9 |
| - |
10 |
| -All three sections — script, styles and markup — are optional. |
11 |
| - |
12 |
| -```svelte |
13 |
| -<script> |
14 |
| - // logic goes here |
15 |
| -</script> |
16 |
| -
|
17 |
| -<!-- markup (zero or more items) goes here --> |
18 |
| -
|
19 |
| -<style> |
20 |
| - /* styles go here */ |
21 |
| -</style> |
22 |
| -``` |
23 |
| - |
24 |
| -## `<script>` |
25 |
| - |
26 |
| -A `<script>` block contains JavaScript (or TypeScript, when adding the `lang="ts"` attribute) that runs when a component instance is created. Variables declared (or imported) at the top level are 'visible' from the component's markup. |
27 |
| - |
28 | 5 | ### Public API of a component
|
29 | 6 |
|
30 | 7 | Svelte uses the `$props` rune to declare _properties_ or _props_, which means describing the public interface of the component which becomes accessible to consumers of the component.
|
@@ -160,43 +137,3 @@ If you'd like to react to changes to a prop, use the `$derived` or `$effect` run
|
160 | 137 | ```
|
161 | 138 |
|
162 | 139 | For more information on reactivity, read the documentation around runes.
|
163 |
| - |
164 |
| -## `<script module>` |
165 |
| - |
166 |
| -A `<script>` tag with a `module` attribute runs once when the module first evaluates, rather than for each component instance. Values declared in this block are accessible from a regular `<script>` (and the component markup) but not vice versa. |
167 |
| - |
168 |
| -You can `export` bindings from this block, and they will become exports of the compiled module. |
169 |
| - |
170 |
| -You cannot `export default`, since the default export is the component itself. |
171 |
| - |
172 |
| -```svelte |
173 |
| -<script module> |
174 |
| - let totalComponents = 0; |
175 |
| -
|
176 |
| - // the export keyword allows this function to imported with e.g. |
177 |
| - // `import Example, { alertTotal } from './Example.svelte'` |
178 |
| - export function alertTotal() { |
179 |
| - alert(totalComponents); |
180 |
| - } |
181 |
| -</script> |
182 |
| -
|
183 |
| -<script> |
184 |
| - totalComponents += 1; |
185 |
| - console.log(`total number of times this component has been created: ${totalComponents}`); |
186 |
| -</script> |
187 |
| -``` |
188 |
| - |
189 |
| -## `<style>` |
190 |
| - |
191 |
| -CSS inside a `<style>` block will be scoped to that component. |
192 |
| - |
193 |
| -```svelte |
194 |
| -<style> |
195 |
| - p { |
196 |
| - /* this will only affect <p> elements in this component */ |
197 |
| - color: burlywood; |
198 |
| - } |
199 |
| -</style> |
200 |
| -``` |
201 |
| - |
202 |
| -For more information regarding styling, read the documentation around [styles and classes](styles-and-classes). |
0 commit comments