Skip to content

Instantly share code, notes, and snippets.

@b3b00
Last active August 7, 2019 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b3b00/df65737b5a0ebbccf4f8b16d2518f150 to your computer and use it in GitHub Desktop.
Save b3b00/df65737b5a0ebbccf4f8b16d2518f150 to your computer and use it in GitHub Desktop.
svelte js navigation
<script>
let name = 'world';
const states = {
FIRST : "first",
SECOND : "second",
THIRD:"third"
}
let state = states.FIRST;
let val = 0;
import First from './First.svelte';
import Second from './Second.svelte';
import Third from './Third.svelte';
function goSecond(data) {
val = data.detail.value;
state=states.SECOND;
}
function goFirst(data) {
val = data.detail.value;
state=states.FIRST;
}
function gothird(data) {
val = data.detail.value;
state=states.THIRD;
}
</script>
{#if (state==states.FIRST)}
<p>
first
</p>
<First on:next={goSecond} v={val}/>
{:else if (state == states.SECOND) }
<p>
second
</p>
<Second on:back={goFirst} on:next={gothird} v={val}/>
{:else if (state == states.THIRD) }
<p>
third
</p>
<Third on:back={goSecond} v={val}/>
{:else}
<h1 color="red">not found</h1>
{/if}
<script>
import { createEventDispatcher } from 'svelte';
export let v = 0;
const dispatch = createEventDispatcher();
function nxt() {
let rnd = Math.random();
rnd = Math.ceil(rnd *100)
dispatch('next',{value:rnd});
}
</script>
<button on:click={nxt}>forward</button>
<h1>
{v}
</h1>
import App from './App.svelte';
var app = new App({
target: document.body
});
export default app;
<script>
import { createEventDispatcher } from 'svelte';
export let v = 0;
const dispatch = createEventDispatcher();
function bck() {
let rnd = Math.random();
rnd = Math.ceil(rnd *100)
dispatch('back',{value:rnd});
}
function forw() {
let rnd = Math.random();
rnd = Math.ceil(rnd *100)
dispatch('next',{value:rnd});
}
</script>
<button on:click={bck}>backward</button>
<button on:click={forw}>forward</button>
<h1>
{v}
</h1>
<script>
import { createEventDispatcher } from 'svelte';
export let v = 0;
const dispatch = createEventDispatcher();
function bck() {
let rnd = Math.random();
rnd = Math.ceil(rnd *100)
dispatch('back',{value:rnd});
}
</script>
<button on:click={bck}>backward</button>
<h1>
{v}
</h1>
@b3b00
Copy link
Author

b3b00 commented Aug 7, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment