vue.js實(shí)現(xiàn)備忘錄功能的方法
這個(gè)vue實(shí)現(xiàn)備忘錄的功能demo是K在github上找到的,K覺(jué)得這是一個(gè)用來(lái)對(duì)vue.js入門(mén)的一個(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在table表中懸浮顯示數(shù)據(jù)及右鍵菜單
這篇文章主要為大家詳細(xì)介紹了vue在table表中懸浮顯示數(shù)據(jù)及右鍵菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
VUE 實(shí)現(xiàn)一個(gè)簡(jiǎn)易老虎機(jī)的項(xiàng)目實(shí)踐
老虎機(jī)在很多地方都可以見(jiàn)到,可以設(shè)置中獎(jiǎng)位置,以及中獎(jiǎng)回調(diào),本文主要介紹了VUE 實(shí)現(xiàn)一個(gè)簡(jiǎn)易老虎機(jī)的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
深入探討Vue計(jì)算屬性與監(jiān)聽(tīng)器的區(qū)別和用途
在Vue的開(kāi)發(fā)中,計(jì)算屬性(Computed Properties)和監(jiān)聽(tīng)器(Watchers)是兩種非常重要的概念,它們都用于響應(yīng)式地處理數(shù)據(jù)變化,本文將帶你深入了解計(jì)算屬性和監(jiān)聽(tīng)器的區(qū)別,以及在何時(shí)使用它們,感興趣的朋友可以參考下2023-09-09
elementUI vue this.$confirm 和el-dialog 彈出框 移動(dòng) 示例demo
這篇文章主要介紹了elementUI vue this.$confirm 和el-dialog 彈出框 移動(dòng) 示例demo,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
vue使用element-ui按需引入時(shí)踩過(guò)的那些坑
Element-UI是基于vue實(shí)現(xiàn)的一套不依賴業(yè)務(wù)的UI組件庫(kù),提供了豐富的PC端組件,減少用戶對(duì)常用組件的封裝,降低了開(kāi)發(fā)的難易程度,下面這篇文章主要給大家介紹了關(guān)于vue使用element-ui按需引入時(shí)踩過(guò)的那些坑,需要的朋友可以參考下2022-05-05
vue單頁(yè)開(kāi)發(fā)父子組件傳值思路詳解
這篇文章主要介紹了vue單頁(yè)開(kāi)發(fā)父子組件傳值思路詳解,本文是小編抽空整理的思路,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05

