Skip to content

Instantly share code, notes, and snippets.

@luanlmd
Created July 28, 2017 19:50
Show Gist options
  • Select an option

  • Save luanlmd/af7ccaffdcd3694511429d8557bb284f to your computer and use it in GitHub Desktop.

Select an option

Save luanlmd/af7ccaffdcd3694511429d8557bb284f to your computer and use it in GitHub Desktop.
VueJS simplest todo ever
<html>
<head>
<title>VueJS simplest todo ever</title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="(item, index) in todos" :id="'item-'+index">
{{ item }} <button @click="remove(index)">x</button>
</li>
</ul>
<input type="text" v-model="todo" placeholder="todo" />
<button @click="add">add</button>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
todos : [],
todo: ""
},
methods:
{
add: function() {
this.todos.push(this.todo)
this.todo = ''
},
remove: function(index) {
this.todos.splice(index, 1)
}
},
watch: {
todos: function(){
localStorage.todos = JSON.stringify(this.todos)
}
},
mounted: function() {
this.todos = JSON.parse(localStorage.todos) || []
}
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment