Skip to content

Instantly share code, notes, and snippets.

@paulocoghi
Last active March 28, 2018 14:24
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 paulocoghi/e2b577ec8d07a248577cfbd8ef59bfe3 to your computer and use it in GitHub Desktop.
Save paulocoghi/e2b577ec8d07a248577cfbd8ef59bfe3 to your computer and use it in GitHub Desktop.
Without multiple sub-components and store variables, everything works

Without multiple sub-components and store variables, everything works

<input bind:value='newTodo' placeholder='buy milk'>
<button on:click='push("$todos", newTodo)'>add todo</button>
<ul>
{{#each ( $todos || [] ) as todo, i}}
<li>
<button on:click='splice("$todos", i, 1)'>x</button>
{{todo}}
</li>
{{/each}}
</ul>
<style>
ul {
list-style: none;
padding: 0;
}
li button {
color: rgb(200,0,0);
background: rgba(200,0,0,0.1);
border-color: rgba(200,0,0,0.2);
padding: 0.2em 0.5em;
}
</style>
<script>
import { Store } from 'svelte/store.js';
import { push, splice } from 'svelte-extras';
const store = new Store( {} );
export default {
data: function () {
return {
newTodo: ''
};
},
methods: {
push,
splice
},
store: () => store,
oncreate: function () {
this.store.set( { todos: [ 'Item 1', 'Item 2', 'Item 3' ] } )
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment