npm create svelte@latest [my-app]
cd my-app
yarn && yarn dev
$: doubled = count * 2
Block:
$: {
console.log(`the count is ${count}`);
alert(`I SAID THE COUNT IS ${count}`);
}
<input bind:value={value}>
<input bind:value>
<input type=checkbox bind:checked={didApprove}>
{#if expression}...{:else if expression}...{/if}
{#each arrayLikeObject as name, index (keyField)}...{/each}
{#await expression}...{:then name}...{:catch name}...{/await}
{@html expression}
{@debug var1, var2, ..., varN}
<script>
export let myProp
</script>
<button on:click={handleClick}>Click Me</button>
<div on:mousemove={event => m = { x: event.clientX, y: event.clientY }} />
Modifiers: https://svelte.dev/tutorial/event-modifiers
<button on:click|preventDefault={handleClick}>Click Me</button>
https://svelte.dev/tutorial/dom-event-forwarding
<div class="box">
<slot></slot>
</div>
import { onMount } from 'svelte'
onMount(async () => {
const module = await import('my-non-ssr-component')
MyComponent = module.default
})
https://flaviocopes.com/svelte-state-management/
import { setContext } from 'svelte'
setContext('someKey', someObject)
import { getContext } from 'svelte'
const someObject = getContext('someKey')
https://svelte.dev/tutorial/writable-stores
stores.js:
import { writable } from 'svelte/store'
export const count = writable(0)
Component:
import { count } from './stores.js'
<p>The count is {$count}</p>
https://svelte.dev/tutorial/module-exports
<script context="module">
https://github.com/YogliB/svelte-component-template
npm init svelte@next MYAPP
cd MYAPP
yarn && yarn dev
https://sapper.svelte.dev/docs#Preloading
<script context="module">
export function preload({ params, query }) {
return this.fetch(`blog.json`).then(r => r.json()).then(posts => {
return { posts }
})
}
</script>
https://sapper.svelte.dev/docs#Stores
-
https://github.com/beyonk-adventures/now-sapper-demo
npx degit beyonk-adventures/now-sapper-demo MYAPP cd MYAPP yarn && yarn dev
To fix 500 ECONNREFUSED error on Zeit Now v2:
export default function zeitNowUrl (path, host = '') {
const baseUrl = (!process.browser && process.env.NOW_REGION)
? `https://${host}`
: ''
return `${baseUrl}/${path}`
}
...and then in each page:
<script context="module">
import zeitNowUrl from '../../lib/zeitNowUrl'
export function preload({ host, params, query }) {
return this.fetch(zeitNowUrl('blog.json', host)).then(r => r.json()).then(posts => {
return { posts };
});
}
</script>