|
1 | 1 | <StepHead>
|
2 | 2 |
|
3 | 3 | ```svelte App.svelte
|
4 |
| -<h1>Hello Svelte</h1> |
| 4 | +<h1>Svelte</h1> |
5 | 5 | ```
|
6 | 6 |
|
7 | 7 | </StepHead>
|
8 | 8 |
|
9 |
| -^0 |
| 9 | +Svelte uses a custom file format, similar to HTML |
10 | 10 |
|
11 | 11 | <StepHead>
|
12 | 12 |
|
|
15 | 15 | let name = "Svelte"
|
16 | 16 | </script>
|
17 | 17 |
|
18 |
| -<h1>{name}</h1> |
| 18 | +<h1>Hello {name}</h1> |
19 | 19 | ```
|
20 | 20 |
|
21 | 21 | </StepHead>
|
22 | 22 |
|
23 |
| -^1 |
| 23 | +You can use curly braces to render data. |
24 | 24 |
|
25 | 25 | <StepHead>
|
26 | 26 |
|
27 | 27 | ```svelte App.svelte
|
| 28 | +
|
| 29 | +``` |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +```svelte MyComponent.svelte |
| 34 | +<div> |
| 35 | + <button>Hello</button> |
| 36 | +</div> |
| 37 | +``` |
| 38 | + |
| 39 | +</StepHead> |
| 40 | + |
| 41 | +Each svelte file is a different component |
| 42 | + |
| 43 | +<StepHead> |
| 44 | + |
| 45 | +```svelte App.svelte focus=2,5,6 |
| 46 | +<script> |
| 47 | + import MyComponent from './MyComponent.svelte' |
| 48 | +</script> |
| 49 | +
|
| 50 | +<MyComponent /> |
| 51 | +<MyComponent /> |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +```svelte MyComponent.svelte focus=2[1] |
| 57 | +<div> |
| 58 | + <button>Hello</button> |
| 59 | +</div> |
| 60 | +``` |
| 61 | + |
| 62 | +</StepHead> |
| 63 | + |
| 64 | +You can add imports inside the script tag |
| 65 | + |
| 66 | +<StepHead> |
| 67 | + |
| 68 | +```svelte App.svelte focus=5[14:26],6[14:28] |
28 | 69 | <script>
|
29 | 70 | import MyComponent from './MyComponent.svelte'
|
30 | 71 | </script>
|
|
33 | 74 | <MyComponent name="Ronaldo" />
|
34 | 75 | ```
|
35 | 76 |
|
| 77 | +--- |
| 78 | + |
| 79 | +```svelte MyComponent.svelte focus=2[1] |
| 80 | +<div> |
| 81 | + <button>Hello</button> |
| 82 | +</div> |
| 83 | +``` |
| 84 | + |
36 | 85 | </StepHead>
|
37 | 86 |
|
38 |
| -^2 |
| 87 | +To share data with children components |
39 | 88 |
|
40 | 89 | <StepHead>
|
41 | 90 |
|
42 |
| -```svelte App.svelte focus=2 |
| 91 | +```svelte App.svelte focus=5[14:26],6[14:28] |
| 92 | +<script> |
| 93 | + import MyComponent from './MyComponent.svelte' |
| 94 | +</script> |
43 | 95 |
|
| 96 | +<MyComponent name="Messi" /> |
| 97 | +<MyComponent name="Ronaldo" /> |
44 | 98 | ```
|
45 | 99 |
|
46 | 100 | ---
|
47 | 101 |
|
48 |
| -```svelte MyComponent.svelte |
| 102 | +```svelte MyComponent.svelte focus=2,6 |
49 | 103 | <script>
|
50 | 104 | export let name;
|
51 | 105 | </script>
|
|
57 | 111 |
|
58 | 112 | </StepHead>
|
59 | 113 |
|
60 |
| -^3 Each svelte file is a different component |
| 114 | +you can create props using the export keyword |
61 | 115 |
|
62 | 116 | <StepHead>
|
63 | 117 |
|
64 |
| -```svelte App.svelte |
65 |
| -
|
66 |
| -``` |
67 |
| - |
68 |
| -```svelte MyComponent.svelte focus=2 active |
| 118 | +```svelte MyComponent.svelte focus=3,8 |
69 | 119 | <script>
|
70 | 120 | export let name;
|
| 121 | + let goalCount = 2; |
71 | 122 | </script>
|
72 | 123 |
|
73 | 124 | <div>
|
74 | 125 | <button>{name}</button>
|
| 126 | + {"⚽".repeat(goalCount)} |
75 | 127 | </div>
|
76 | 128 | ```
|
77 | 129 |
|
78 | 130 | </StepHead>
|
79 | 131 |
|
80 |
| -^4 You can declare props with the export keyword. |
| 132 | +Inside curly braces you can put any javascript expression |
81 | 133 |
|
82 | 134 | <StepHead>
|
83 | 135 |
|
84 |
| -```svelte MyComponent.svelte focus=3,8 |
| 136 | +```svelte MyComponent.svelte focus=11[11:32] |
85 | 137 | <script>
|
86 | 138 | export let name;
|
87 | 139 | let goalCount = 2;
|
| 140 | +
|
| 141 | + function handleClick() { |
| 142 | + // TODO |
| 143 | + } |
88 | 144 | </script>
|
89 | 145 |
|
90 | 146 | <div>
|
91 |
| - <button>{name}</button> |
| 147 | + <button on:click={handleClick}>{name}</button> |
92 | 148 | {"⚽".repeat(goalCount)}
|
93 | 149 | </div>
|
94 | 150 | ```
|
95 | 151 |
|
96 | 152 | </StepHead>
|
97 | 153 |
|
98 |
| -^5 using js inside curly braces |
| 154 | +With the on directive you can listen to events |
99 | 155 |
|
100 | 156 | <StepHead>
|
101 | 157 |
|
|
117 | 173 |
|
118 | 174 | </StepHead>
|
119 | 175 |
|
120 |
| -^6 adding event handlers |
| 176 | +here, when the goalcount changes after a click, svelte will rerender the component (show) |
121 | 177 |
|
122 | 178 | <StepHead>
|
123 | 179 |
|
|
143 | 199 |
|
144 | 200 | </StepHead>
|
145 | 201 |
|
146 |
| -^7 firing custom events |
| 202 | +You can also use a custom event to share data with the parent |
147 | 203 |
|
148 | 204 | <StepHead>
|
149 | 205 |
|
150 |
| -```svelte App.svelte focus=4:6,9[27:42],10[29:44] |
| 206 | +```svelte App.svelte focus=9[27:42],10[29:44] |
151 | 207 | <script>
|
152 | 208 | import MyComponent from './MyComponent.svelte'
|
153 | 209 |
|
|
160 | 216 | <MyComponent name="Ronaldo" on:goal={onGoal}/>
|
161 | 217 | ```
|
162 | 218 |
|
| 219 | +--- |
| 220 | + |
| 221 | +```svelte MyComponent.svelte focus=10 |
| 222 | +
|
| 223 | +``` |
| 224 | + |
163 | 225 | </StepHead>
|
164 | 226 |
|
165 |
| -handling custom events |
| 227 | +and use the on directive again to handle it |
166 | 228 |
|
167 | 229 | <StepHead>
|
168 | 230 |
|
169 |
| -```svelte App.svelte focus=3,9,12 |
| 231 | +```svelte App.svelte focus=4:6,9[27:42],10[29:44] |
170 | 232 | <script>
|
171 | 233 | import MyComponent from './MyComponent.svelte'
|
172 |
| - import MyBox from './MyBox.svelte' |
| 234 | +
|
173 | 235 | function onGoal(event) {
|
174 | 236 | console.log(event.detail.player)
|
175 | 237 | }
|
176 | 238 | </script>
|
177 | 239 |
|
178 |
| -<MyBox> |
179 |
| - <MyComponent name="Messi" on:goal={onGoal}/> |
180 |
| - <MyComponent name="Ronaldo" on:goal={onGoal}/> |
181 |
| -</MyBox> |
182 |
| -
|
| 240 | +<MyComponent name="Messi" on:goal={onGoal}/> |
| 241 | +<MyComponent name="Ronaldo" on:goal={onGoal}/> |
183 | 242 | ```
|
184 | 243 |
|
185 |
| ---- |
186 |
| - |
187 | 244 | </StepHead>
|
188 | 245 |
|
189 |
| -adding style |
| 246 | +and do something with the data |
190 | 247 |
|
191 | 248 | <StepHead>
|
192 | 249 |
|
193 |
| -```svelte App.svelte focus=9,12 |
194 |
| -
|
195 |
| -``` |
196 |
| - |
197 |
| ---- |
198 |
| - |
199 |
| -```svelte MyBox.svelte |
200 |
| -<div> |
201 |
| - something |
202 |
| -</div> |
203 |
| -
|
204 |
| -<style> |
205 |
| - div { |
206 |
| - border: 8px solid hotpink; |
| 250 | +```svelte App.svelte focus=3 |
| 251 | +<script> |
| 252 | + import MyComponent from './MyComponent.svelte' |
| 253 | + const players = ['Messi', 'Ronaldo', 'Laspada'] |
| 254 | + function onGoal(event) { |
| 255 | + console.log(event.detail.player) |
207 | 256 | }
|
208 |
| -</style> |
209 |
| -``` |
210 |
| - |
211 |
| -</StepHead> |
212 |
| - |
213 |
| -... |
214 |
| - |
215 |
| -<StepHead> |
216 |
| - |
217 |
| -```svelte App.svelte focus=9,12 |
218 |
| -
|
219 |
| -``` |
220 |
| - |
221 |
| ---- |
222 |
| - |
223 |
| -```svelte MyBox.svelte focus=2 |
224 |
| -<div> |
225 |
| - <slot /> |
226 |
| -</div> |
| 257 | +</script> |
227 | 258 |
|
228 |
| -<style> |
229 |
| - div { |
230 |
| - border: 8px solid hotpink; |
231 |
| - } |
232 |
| -</style> |
| 259 | +<MyComponent name="Messi" on:goal={onGoal}/> |
| 260 | +<MyComponent name="Ronaldo" on:goal={onGoal}/> |
233 | 261 | ```
|
234 | 262 |
|
235 | 263 | </StepHead>
|
236 | 264 |
|
237 |
| -using slot |
| 265 | +If you want to render a list |
238 | 266 |
|
239 | 267 | <StepHead>
|
240 | 268 |
|
241 |
| -```svelte App.svelte focus=4 |
| 269 | +```svelte App.svelte focus=3,9,10[16:28],11 |
242 | 270 | <script>
|
243 | 271 | import MyComponent from './MyComponent.svelte'
|
244 |
| - import MyBox from './MyBox.svelte' |
245 | 272 | const players = ['Messi', 'Ronaldo', 'Laspada']
|
246 | 273 | function onGoal(event) {
|
247 | 274 | console.log(event.detail.player)
|
248 | 275 | }
|
249 | 276 | </script>
|
250 | 277 |
|
251 |
| -<MyBox> |
252 |
| - <MyComponent name="Messi" on:goal={onGoal}/> |
253 |
| - <MyComponent name="Ronaldo" on:goal={onGoal}/> |
254 |
| -</MyBox> |
| 278 | +{#each players as player} |
| 279 | + <MyComponent name={player} on:goal={onGoal}/> |
| 280 | +{/each} |
255 | 281 | ```
|
256 | 282 |
|
257 | 283 | </StepHead>
|
258 | 284 |
|
259 |
| -rendering lists |
| 285 | +you can use the each block. |
260 | 286 |
|
261 | 287 | <StepHead>
|
262 | 288 |
|
263 |
| -```svelte App.svelte focus=4,11,12[18:30],13 |
| 289 | +```svelte App.svelte focus=9:13 |
264 | 290 | <script>
|
265 | 291 | import MyComponent from './MyComponent.svelte'
|
266 |
| - import MyBox from './MyBox.svelte' |
267 | 292 | const players = ['Messi', 'Ronaldo', 'Laspada']
|
268 | 293 | function onGoal(event) {
|
269 | 294 | console.log(event.detail.player)
|
270 | 295 | }
|
271 | 296 | </script>
|
272 | 297 |
|
273 |
| -<MyBox> |
274 |
| - {#each players as player} |
275 |
| - <MyComponent name={player} on:goal={onGoal}/> |
276 |
| - {/each} |
277 |
| -</MyBox> |
| 298 | +<style> |
| 299 | + div { |
| 300 | + border: 8px solid hotpink; |
| 301 | + } |
| 302 | +</style> |
| 303 | +
|
| 304 | +<div> |
| 305 | +{#each players as player} |
| 306 | + <MyComponent name={player} on:goal={onGoal}/> |
| 307 | +{/each} |
| 308 | +</div> |
| 309 | +``` |
| 310 | + |
| 311 | +</StepHead> |
| 312 | + |
| 313 | +Svelte also support style tags, |
| 314 | + |
| 315 | +<StepHead> |
| 316 | + |
| 317 | +```svelte App.svelte focus=10:12,15,19 |
| 318 | +
|
278 | 319 | ```
|
279 | 320 |
|
280 | 321 | </StepHead>
|
281 | 322 |
|
282 |
| -... |
| 323 | +Here the style only applies to the div from the current component and not others. |
0 commit comments