You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: site/content/docs/01-component-format.md
+12-12
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Components are the building blocks of Svelte applications. They are written into
8
8
9
9
All three sections — script, styles and markup — are optional.
10
10
11
-
```html
11
+
```sv
12
12
<script>
13
13
// logic goes here
14
14
</script>
@@ -30,7 +30,7 @@ A `<script>` block contains JavaScript that runs when a component instance is cr
30
30
31
31
Svelte uses the `export` keyword to mark a variable declaration as a *property* or *prop*, which means it becomes accessible to consumers of the component (see the section on [attributes and props](docs#Attributes_and_props) for more information).
32
32
33
-
```html
33
+
```sv
34
34
<script>
35
35
export let foo;
36
36
@@ -46,7 +46,7 @@ You can specify a default initial value for a prop. It will be used if the compo
46
46
47
47
In development mode (see the [compiler options](docs#svelte_compile)), a warning will be printed if no default initial value is provided and the consumer does not specify a value. To squelch this warning, ensure that a default initial value is specified, even if it is `undefined`.
48
48
49
-
```html
49
+
```sv
50
50
<script>
51
51
export let bar = 'optional default initial value';
52
52
export let baz = undefined;
@@ -57,7 +57,7 @@ In development mode (see the [compiler options](docs#svelte_compile)), a warning
57
57
58
58
If you export a `const`, `class` or `function`, it is readonly from outside the component. Function *expressions* are valid props, however.
59
59
60
-
```html
60
+
```sv
61
61
<script>
62
62
// these are readonly
63
63
export const thisIs = 'readonly';
@@ -75,7 +75,7 @@ If you export a `const`, `class` or `function`, it is readonly from outside the
75
75
76
76
You can use reserved words as prop names.
77
77
78
-
```html
78
+
```sv
79
79
<script>
80
80
let className;
81
81
@@ -95,7 +95,7 @@ Update expressions (`count += 1`) and property assignments (`obj.x = y`) have th
95
95
96
96
Because Svelte's reactivity is based on assignments, using array methods like `.push()` and `.splice()` won't automatically trigger updates. Options for getting around this can be found in the [tutorial](tutorial/updating-arrays-and-objects).
97
97
98
-
```html
98
+
```sv
99
99
<script>
100
100
let count = 0;
101
101
@@ -113,7 +113,7 @@ Because Svelte's reactivity is based on assignments, using array methods like `.
113
113
114
114
Any top-level statement (i.e. not inside a block or a function) can be made reactive by prefixing it with the `$:`[JS label syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label). Reactive statements run immediately before the component updates, whenever the values that they depend on have changed.
115
115
116
-
```html
116
+
```sv
117
117
<script>
118
118
export let title;
119
119
@@ -132,7 +132,7 @@ Any top-level statement (i.e. not inside a block or a function) can be made reac
132
132
133
133
If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf.
134
134
135
-
```html
135
+
```sv
136
136
<script>
137
137
export let num;
138
138
@@ -157,7 +157,7 @@ Note that the store must be declared at the top level of the component — not i
157
157
158
158
Local variables (that do not represent store values) must *not* have a `$` prefix.
159
159
160
-
```html
160
+
```sv
161
161
<script>
162
162
import { writable } from 'svelte/store';
163
163
@@ -199,7 +199,7 @@ You cannot `export default`, since the default export is the component itself.
199
199
200
200
> Variables defined in `module` scripts are not reactive — reassigning them will not trigger a rerender even though the variable itself will update. For values shared between multiple components, consider using a [store](docs#svelte_store).
201
201
202
-
```html
202
+
```sv
203
203
<script context="module">
204
204
let totalComponents = 0;
205
205
@@ -225,7 +225,7 @@ CSS inside a `<style>` block will be scoped to that component.
225
225
226
226
This works by adding a class to affected elements, which is based on a hash of the component styles (e.g. `svelte-123xyz`).
227
227
228
-
```html
228
+
```sv
229
229
<style>
230
230
p {
231
231
/* this will only affect <p> elements in this component */
@@ -238,7 +238,7 @@ This works by adding a class to affected elements, which is based on a hash of t
238
238
239
239
To apply styles to a selector globally, use the `:global(...)` modifier.
0 commit comments