vue.js實(shí)現(xiàn)備忘錄功能的方法
這個(gè)vue實(shí)現(xiàn)備忘錄的功能demo是K在github上找到的,K覺(jué)得這是一個(gè)用來(lái)對(duì)vue.js入門的一個(gè)非常簡(jiǎn)單的demo,所以拿在這里共享一下。
(尊重他人勞動(dòng)成果,從小事做起~ demo原github地址:https://github.com/vuejs/vue)
一、實(shí)現(xiàn)效果
二、代碼展示
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>備忘錄</title> <link rel="stylesheet" type="text/css" href="css/index.css" rel="external nofollow" /> <style>[v-cloak] { display: none; }</style> </head> <body> <section class="todoapp"> <header class="header"> <h1>todos</h1> <input class="new-todo" autofocus autocomplete="off" placeholder="What needs to be done?" v-model="newTodo" @keyup.enter="addTodo"> </header> <section class="main" v-show="todos.length" v-cloak> <input class="toggle-all" type="checkbox" v-model="allDone"> <ul class="todo-list"> <li v-for="todo in filteredTodos" class="todo" :key="todo.id" :class="{ completed: todo.completed, editing: todo == editedTodo }"> <div class="view"> <input class="toggle" type="checkbox" v-model="todo.completed"> <label @dblclick="editTodo(todo)">{{ todo.title }}</label> <button class="destroy" @click="removeTodo(todo)"></button> </div> <input class="edit" type="text" v-model="todo.title" v-todo-focus="todo == editedTodo" @blur="doneEdit(todo)" @keyup.enter="doneEdit(todo)" @keyup.esc="cancelEdit(todo)"> </li> </ul> </section> <footer class="footer" v-show="todos.length" v-cloak> <span class="todo-count"> <strong>{{ todos.length }}</strong> {{ remaining | pluralize }} left </span> <ul class="filters"> <li><a href="#/all" rel="external nofollow" :class="{ selected: visibility == 'all' }">All</a></li> <li><a href="#/active" rel="external nofollow" :class="{ selected: visibility == 'active' }">Active</a></li> <li><a href="#/completed" rel="external nofollow" :class="{ selected: visibility == 'completed' }">Completed</a></li> </ul> <button class="clear-completed" @click="removeCompleted" v-show="todos.length > remaining"> Clear completed </button> </footer> </section> <footer class="info"> <p>雙擊編輯一條備忘錄</p> </footer> </body> <script language="JavaScript" src="js/director.js"></script> <script language="JavaScript" src="js/vue.js"></script> <script language="JavaScript" src="js/index_vue.js"></script> </html>
// 本地緩存設(shè)置 // 防止頁(yè)面關(guān)閉后,數(shù)據(jù)全部丟失的問(wèn)題 var STORAGE_KEY = 'todos-vuejs-2.0' var todoStorage = { // 獲取本地存儲(chǔ)中的內(nèi)容 fetch:function(){ // JSON.parse()解析一個(gè)json字符串 // localStorage.getItem 從本地存儲(chǔ)中獲取STORAGE_KEY字段的值 var todos = JSON.parse(localStorage.getItem(STORAGE_KEY)||'[]'); // foreach遍歷todos,兩個(gè)參數(shù)分別為遍歷出的每一個(gè)子單元及對(duì)應(yīng)的索引 todos.forEach(function(todo,index){ todo.id = index; }) todoStorage.uid = todos.length; return todos; }, // 保存時(shí)將內(nèi)容寫(xiě)進(jìn)本地存儲(chǔ) save:function(todos){ // localStorage.setItem 將todos轉(zhuǎn)化成字符串存入本地存儲(chǔ),鍵名為STORAGE_KEY localStorage.setItem(STORAGE_KEY,JSON.stringify(todos)) } } // 可視化狀態(tài)過(guò)濾設(shè)置 // 包括全選(all)、選擇未完成(active)、選擇已完成(completed) var filters = { all:function(todos){ return todos; }, // filter() 方法創(chuàng)建一個(gè)新的數(shù)組,新數(shù)組中的元素是通過(guò)檢查指定數(shù)組中符合條件的所有元素。 active:function(todos){ return todos.filter(function(todo){ return !todo.completed; }) }, completed:function(todos){ return todos.filter(function(todo){ return todo.completed; }) } } // vue實(shí)例化 var app = new Vue({ // data 參數(shù)設(shè)置 data:{ todos:todoStorage.fetch(), newTodo:'', editedTodo:null, visibility:'all' }, // watch 監(jiān)視todos在本地儲(chǔ)存中的變化 watch:{ todos:{ handler:function(todos){ todoStorage.save(todos) }, deep:true } }, // computed 檢測(cè)數(shù)據(jù)發(fā)生變動(dòng)時(shí)執(zhí)行函數(shù) computed:{ filteredTodos:function(){ return filters[this.visibility](this.todos) }, remaining:function(){ return filters.active(this.todos).length }, allDone:{ get:function(){ return this.remaining === 0 }, set:function(value){ this.todos.forEach(function(todo){ todo.completed = value }) } } }, // methods 方法設(shè)置 methods:{ addTodo:function(){ var value = this.newTodo && this.newTodo.trim() if(!value){ return; } this.todos.push({ id:todoStorage.uid++, title:value, completed:false }) this.newTodo = '' }, removeTodo:function(todo){ this.todos.splice(this.todos.indexOf(todo),1) }, editTodo:function(todo){ this.beforeEditCache = todo.title; this.editedTodo = todo }, doneEdit:function(todo){ if(!this.editedTodo){ return; }; this.editedTodo = null; todo.title = todo.title.trim() if(!todo.title){ this.removeTodo(todo) } }, cancelEdit:function(todo){ this.editedTodo = null; todo.title = this.beforeEditCache }, removeCompleted:function(){ this.todos = filters.active(this.todos) } }, directives:{ 'todo-focus':function(el,binding){ if(binding.value){ el.focus() } } } }) // hashchange URL的片段標(biāo)識(shí)符更改觸發(fā) function onHashChange(){ var visbility = window.location.hash.replace(/#\/?/, ''); if(filters[visbility]){ app.visibility = visbility }else{ window.location.hash = ''; app.visbility = 'all' } } window.addEventListener('hashchange',onHashChange) onHashChange() // mount 手動(dòng)掛載 app.$mount('.todoapp')
以上這篇vue.js實(shí)現(xiàn)備忘錄功能的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)token登錄驗(yàn)證的完整實(shí)例
最近公司新啟動(dòng)了個(gè)項(xiàng)目,用的是vue框架在做,下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)token登錄驗(yàn)證的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04利用vue.js把靜態(tài)json綁定bootstrap的table方法
今天小編就為大家分享一篇利用vue.js把靜態(tài)json綁定bootstrap的table方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08VUE跳轉(zhuǎn)外部鏈接與網(wǎng)頁(yè)的方法示例
這篇文章主要給大家介紹了關(guān)于VUE跳轉(zhuǎn)外部鏈接與網(wǎng)頁(yè)的方法,記錄一下在vue項(xiàng)目中如何實(shí)現(xiàn)跳轉(zhuǎn)到一個(gè)新頁(yè)面,需要的朋友可以參考下2023-06-06詳解Vue demo實(shí)現(xiàn)商品列表的展示
這篇文章主要介紹了Vue demo實(shí)現(xiàn)商品列表的展示,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Vue渲染器設(shè)計(jì)實(shí)現(xiàn)流程詳細(xì)講解
在瀏覽器平臺(tái)上,用它來(lái)渲染其中的真實(shí)DOM元素。渲染器不僅能夠渲染真實(shí)的DOM元素,它還是框架跨平臺(tái)能力的關(guān)鍵。所以在設(shè)計(jì)渲染器的時(shí)候一定要考慮好自定義的能力2023-01-01Vue使用vue-pdf實(shí)現(xiàn)PDF文件預(yù)覽
這篇文章主要為大家詳細(xì)介紹了Vue如何使用vue-pdf實(shí)現(xiàn)PDF文件預(yù)覽的功能,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下2023-03-03Vue利用canvas實(shí)現(xiàn)移動(dòng)端手寫(xiě)板的方法
本篇文章主要介紹了Vue利用canvas實(shí)現(xiàn)移動(dòng)端手寫(xiě)板的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05