Skip to content

Instantly share code, notes, and snippets.

@edwardlorilla
Created November 25, 2017 11:26
Show Gist options
  • Save edwardlorilla/6eb44b6eee9fee0a6333c421dad3449c to your computer and use it in GitHub Desktop.
Save edwardlorilla/6eb44b6eee9fee0a6333c421dad3449c to your computer and use it in GitHub Desktop.
Perform CRUD Operations with Local Storage Data Vuejs
<div id="app">
<input type="text" v-model="todo.title" placeholder="input todo"
v-on:keyup.enter="addTodo"
>
<ul>
<li v-for="(todo, index) in todos">
<input v-if="todo.edit" type="text" v-model="todo.title">
<span v-else>{{todo.title}} </span>
<button @click="removeTodo(index)">&#10006;</button>
<button @click="todo.edit = !todo.edit">&#9998;</button>
</li>
</ul>
</div>
Vue.use(VueLocalStorage);
new Vue({
el: '#app',
data(){
return {
todo: {
title: null,
edit: false
},
todos: null || [],
}
},
watch: {
todos: function(val) {
this.$ls.set('todos', val)
}
},
mounted(){
this.todos = this.$ls.get('todos', this.todos);
var _this = this;
this.$ls.on('todos', function(val) {
_this.todos = val;
});
},
methods:{
addTodo(){
var vm = this
vm.todos.push({title:vm.todo.title,edit: false })
vm.todo = []
},
removeTodo(index){
this.todos.splice(index, 1)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vue-ls.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment