-
Open https://svelte.dev/
-
Run the following commands
npx degit sveltejs/template hello-world cd hello-world npm install npm run dev
- VS Code
- Extensions
- Svelte Snippets
- Svelte for VS Code
- Svelte 3 Snippets
src/main.js
is the starting point for the app- Notice we're referring to a component named App in the import statement
- The App component targets the HTML body
- Open
src/app.svelte
- We have script/template/style
- The template is displaying "Hello {name}"
name
is defined in the script- change the
name
- Add
hero={name: 'John', birth_year: '1999'}
object to App.svelte - Create HeroDetail.svelte
export let hero;
- Add a label and an input
bind:value={hero.name}
- Add HeroDetail to App.svelte
- Bind
hero={hero}
- Bind to the birth_year
- Create the list of heroes with a snippet
- Create
HeroesList.svelte
export let heroes;
- Add the CSS with a snippet
- Show HeroesList in App.svelte (comment out HeroDetail)
- Bind to the
heroes
array - Create
<ul>
- Create
s-each-block
- Create
<li>
- Create
let selectedHero;
on:click={() => selectedHero = hero}
class:selected={selectedHero === hero}
s-if-block
- Show the heroDetail component
- Bind the
hero
toselectedHero
- Stop binding to the HeroesList in App.svelte
- Remove the
export
fromlet heroes
async function getHeroes() { }
- fetch from SWAP (use
sv-url
) - create
let heroesPromise = getHeroes();
- We want to await the answer so let's use
s-await-catch-block
- Move the UL and LI inside the
:then
- Add a Save button to HeroDetail
on:click={saveHero}
import { createEventDispatcher } from "svelte";
- use
s-dispatch
to get the func - use
s-dispatch-event
todispatch('saveHero', hero)
- In
HeroesList
useon:saveHero={save}
function save({detail: heroToSave}) { console.table{heroToSave}; }