A Pen by Edward Lance Lorilla on CodePen.
Created
November 25, 2017 11:26
-
-
Save edwardlorilla/6eb44b6eee9fee0a6333c421dad3449c to your computer and use it in GitHub Desktop.
Perform CRUD Operations with Local Storage Data Vuejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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)">✖</button> | |
<button @click="todo.edit = !todo.edit">✎</button> | |
</li> | |
</ul> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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