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
+46-27
Original file line number
Diff line number
Diff line change
@@ -28,41 +28,60 @@ A `<script>` block contains JavaScript that runs when a component instance is cr
28
28
29
29
---
30
30
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:
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
33
```html
34
34
<script>
35
-
// these properties can be set externally
36
35
exportlet foo;
36
+
37
+
// Values that are passed in as props
38
+
// are immediately available
39
+
console.log({ foo });
40
+
</script>
41
+
```
42
+
43
+
---
44
+
45
+
You can specify a default value, which will be used if the component's consumer doesn't specify a prop.
46
+
47
+
In development mode (see the [compiler options](docs#svelte_compile)), a warning will be printed if no default is provided and the consumer does not specify a value. To squelch this warning, ensure that a default is specified, even if it is `undefined`.
48
+
49
+
```html
50
+
<script>
37
51
exportlet bar ='optional default value';
52
+
exportlet baz =undefined;
53
+
</script>
54
+
```
55
+
56
+
---
38
57
39
-
// you can use export { ... as ... } to have
40
-
// props whose names are reserved keywords
41
-
let clazz;
42
-
export { clazzasclass };
58
+
If you export a `const`, `class` or `function`, it is readonly from outside the component. Function *expressions* are valid props, however.
43
59
44
-
// this property is readonly externally
45
-
exportconstbuzz='buzz';
60
+
```html
61
+
<script>
62
+
// these are readonly
63
+
exportconstthisIs='readonly';
46
64
47
-
// Values that are passed in as props
48
-
// are immediately available
49
-
console.log(foo, bar);
50
-
51
-
// Function expressions can also be props
52
-
exportletformat= (number) => (number.toFixed(2));
53
-
54
-
// Function declarations are added as methods
55
-
// on the component, rather than props
56
-
exportfunctiongreetMethod() {
57
-
alert(`I'm a <${this.constructor.name}>!`);
65
+
exportfunctiongreet(name) {
66
+
alert(`hello ${name}!`);
58
67
}
59
68
60
-
// you can also use export { ... as ... } to have
61
-
// methods whose names are reserved keywords
62
-
functiondel() {
63
-
do_something();
64
-
}
65
-
export { delasdelete };
69
+
// this is a prop
70
+
exportletformat=n=>n.toFixed(2);
71
+
</script>
72
+
```
73
+
74
+
---
75
+
76
+
You can use reserved words as prop names.
77
+
78
+
```html
79
+
<script>
80
+
let className;
81
+
82
+
// creates a `class` property, even
83
+
// though it is a reserved word
84
+
export { classNameasclass };
66
85
</script>
67
86
```
68
87
@@ -81,8 +100,8 @@ Because Svelte's reactivity is based on assignments, using array methods like `.
0 commit comments