• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

vue.js实现备忘录功能的方法

vue 搞代码 4年前 (2022-01-08) 16次浏览 已收录 0个评论

下面小编就为大家带来一篇vue.js实现备忘录功能的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

这个vue实现备忘录的功能demo是K在github上找到的,K觉得这是一个用来对vue.js入门的一个非常简单的demo,所以拿在这里共享一下。

(尊重他人劳动成果,从小事做起~  demo原github地址:https://github.com/vuejs/vue)

一、实现效果

 

二、代码展示

   <title>备忘录</title>[v-cloak] { display: none; } <section class="todoapp"> <header class="header"> <h1>todos</h1></header><section class="main"> <ul class="todo-list"> <li class="todo"> <div class="view"> <label>{{ todo.title }}</label><button class="destroy"></button></div></li></ul></section><footer class="footer"> <span class="todo-count"> <strong>{{ todos.length }}</strong> {{ remaining | pluralize }} left </span><ul class="filters"> <li>All</li><li>Active</li><li>Completed</li></ul><button class="clear-completed"> remaining"> Clear completed </button></footer></section><footer class="info"> <p>双击编辑一条备忘录</p></footer>
 // 本地缓存设置 // 防止页面关闭后,数据全部丢失的问题 var STORAGE_KEY = 'todos-vuejs-2.0' var todoStorage = { // 获取本地存储中的内容 fetch:function(){ // JSON.parse()解析一个json字符串 //  localStorage.getItem 从本地存储中获取STORAGE_KEY字段的值 var todos = JSON.parse(localStorage.getItem(STORAGE_KEY)||'[]'); //  foreach遍历todos,两个参数分别为遍历出的每一个子单元及对应的索引 todos.forEach(function(todo,index){ todo.id = index; }) todoStorage.uid = todos.length; return todos; }, // 保存时将内容写进本地存储 save:function(todos){ // localStorage.setItem 将todos转化成字符串存入本地存储,键名为STORAGE_KEY localStorage.setItem(STORAGE_KEY,JSON.stringify(todos)) } } // 可视化状态过滤设置 //  包括全选(all)、选择未完成(active)、选择已完成(completed) var filters = { all:function(todos){ return todos; }, //  filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。 active:function(todos){ return t<div style="color:transparent">本文来源gaodai.ma#com搞##代!^码网(</div>odos.filter(function(todo){ return !todo.completed; }) }, completed:function(todos){ return todos.filter(function(todo){ return todo.completed; }) } } // vue实例化 var app = new Vue({ //  data 参数设置 data:{ todos:todoStorage.fetch(), newTodo:'', editedTodo:null, visibility:'all' }, //  watch 监视todos在本地储存中的变化 watch:{ todos:{ handler:function(todos){ todoStorage.save(todos) }, deep:true } }, //  computed 检测数据发生变动时执行函数 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 方法设置 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的片段标识符更改触发 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 手动挂载 app.$mount('.todoapp')

以上这篇vue.js实现备忘录功能的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是vue.js实现备忘录功能的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:vue.js实现备忘录功能的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址