一、项目介绍

①地址:http://todomvc.com/

②GitHub下载模板

③通过npm下载模板的样式

④通过npm下载Vuejs

⑤项目文件,主要修改app.js和index.html两个文件

二、使用Vuejs需求实现(主体思路)

①列表渲染

  • 有数据的时候展示出来:v-if 的使用
  1. (function (Vue) {
  2. let todos=[
  3. {id:1,title:'睡觉',completed:true},
  4. {id:2,title:'美食',completed:false},
  5. {id:3,title:'代码',completed:true}
  6. ]
  7. new Vue({
  8. el:'#todoapp',
  9. data:{
  10. todos:todos,
  11. },
  12. })(Vue);
  1. <li v-for="item of todos" v-bind:class='{completed:item.completed}'>
  2. <div class="view">
  3. <input class="toggle" type="checkbox" v-model='item.completed'>
  4. <label>{{item.title}}</label>
  5. <button class="destroy"></button>
  6. </div>
  7. <input class="edit" value="Create a TodoMVC template">
  8. </li>
  • 没有数据的时候隐藏main部分:添加一个不会出现在页面的template模板,并且使用v-if,当todos没有数据的时候,长度为0
  1. <template v-if='todos.length'>
  2. <!-- This section should be hidden by default and shown when there are todos -->
  3. <section class="main"> ..... </section>
  4. <!-- This footer should hidden by default and shown when there are todos -->
  5. <footer class="footer"> .... </footer>
  6. </template>

②添加任务

  • 页面初始化获得焦点:自定义指令,注册一个全局自定义指令 `v-focus`,然后在input里直接使用
  1. // 自定义指令,自动获取焦点
  2. Vue.directive('focus', {
  3. inserted: function (el) {
  4. el.focus();
  5. }
  6. });
  1. <input class="new-todo" placeholder="What needs to be done?" @keyup.enter='addTodo' v-focus>
  • 敲回车添加到任务列表:鼠标抬起注册addTodo事件,追加数据
  • 不允许有非空数据:为空时,return返回
  • 添加完成后清空文本框:令event.target.value= ' '
  1. <header class="header">
  2. <h1>todos</h1>
  3. <input class="new-todo" placeholder="What needs to be done?" @keyup.enter='addTodo'>
  4. </header>
  1. methods:{
  2. // 添加任务
  3. addTodo(event){
  4. let todoText=event.target.value.trim();
  5. if(!todoText.length){
  6. return
  7. }
  8. let id=this.todos[this.todos.length-1].id+1;
  9. this.todos.push({
  10. id:id,
  11. title:todoText,
  12. completed:false,
  13. });
  14. event.target.value='';
  15. },

③标记所有任务完成或者未完成:点击的时候注册toggleAll事件处理函数

  1. <input @click='toggleAll' id="toggle-all" class="toggle-all" type="checkbox">
  1. toggleAll(event){
  2. let checked=event.target.checked;
  3. this.todos.forEach(todo => todo.completed=checked);
  4. },

④任务项

  • 切换任务完成状态:v-bind绑定一个class=“{类名:布尔值}”,当布尔值为true,作用这个类名,当布尔值为false,则去除这个类名
  1. <li v-for="item of todos" v-bind:class='{completed:item.completed}'>
  • 删除单个任务项:@click=‘ removeTodo(index,$event) ’ ,传入两个参数,删除的索引index和事件$event(传参以后,正常的event获取不到),然后处理函数利用数组方法splice操作
  1. <button class="destroy" @click='removeTodo(index,$event)' ></button>
  1. removeTodo(delIndex,event){
  2. this.todos.splice(delIndex,1);
  3. },
  • 双击label进入编辑模式:这里使用一个中间变量currentEditing,默认为null,也就是所有的任务项都没有editing样式,editing的样式取决于中间变量是否等价于当前任务项,当双击的时候,手动把中间量等于双击的当前任务项,这样editing样式就为true,也就是起作用了。
  1. <li v-for="(item,index) of todos" v-bind:class='{completed:item.completed,editing:item===currentEditing}'>
  1. <label @dblclick="currentEditing=item">{{item.title}}</label>
  1. data:{
  2. todos:todos,
  3. currentEditing:null,

⑤编辑任务项

  • 编辑文本框自动获得焦点:局部自定义指令,自动获取焦点‘ editing-focus ’
  1. <input class="edit" :value='item.title' @blur='saveEdit(item,index,$event)' @keyup.enter='saveEdit(item,index,$event)' @keyup.esc='currentEditing=null' v-editing-focus="item===currentEditing">
  1. directives:{
  2. // 局部自定义属性
  3. editingFocus:{
  4. update(el,binding){
  5. if(binding.value){
  6. el.focus();
  7. }
  8. },
  9. },
  10. },
  • 在编辑文本框敲回车后者失去焦点后,如果为空,则直接删除这个item,如果不为空,保存这个数据,并去除editing样式:saveEdit处理函数,传入参数
  • 输入状态按下esc取消编辑:设置默认value属性是item的title,按下esc抬起的时候,令中间变量为null,去除editing样式
  1. <input class="edit" :value='item.title' @blur='saveEdit(item,index,$event)' @keyup.enter='saveEdit(item,index,$event)' @keyup.esc='currentEditing=null'>
  1. saveEdit(item,index,event){
  2. var editText=event.target.value.trim();
  3. // 如果为空,直接删除这个item
  4. if(!editText.length){
  5. return this.todos.splice(index,1);
  6. }
  7. // 如果不为空,修改title的值,然后去除eiditing样式
  8. item.title=editText;
  9. this.currentEditing=null;
  10. },

⑥其他(footer部分)

  • 显示所有未完成任务数:@click=‘ removeAllDone ’ ,处理事件利用数组方法filter过滤未完成数据,然后重新赋值给数据列表
  1. <button class="clear-completed" @click='removeAllDone'>Clear completed</button>
  1. removeAllDone(){
  2. this.todos=this.todos.filter((item,index)=>{
  3. return !item.completed;//return true,即item.completed为false
  4. });
  5. },
  • 清除所有的已完成任务:利用计算属性computed的自定义方法leftCount(参考vue教程--计算属性),还有一种方法就是模板中调用处理函数,处理函数使用for循环来删除,但是删完需要把循环索引i--,但是这种方法没有缓存,每一次使用都要重新调用,推荐使用计算属性,效率更高。
  1. <span class="todo-count"><strong>{{leftCount}}</strong> item left</span>
  1. computed:{
  2. leftCount:function(){
  3. return this.todos.filter(item => !item.completed).length
  4. }
  5. },
  • 将数据持久化到localStorage中(待完成):利用watch功能(配置deep,深度监视),计算属性用于需要在模板中绑定输出值,而watch观察者则用于根据需要数据的改变从而定制特殊功能业务
  • 路由状态切换:data里添加属性filterState默认为‘all’;计算属性computed增加filtertodos方法,过滤不同状态的路由;同时修改列表渲染为遍历filterTodos;在window里添加路由改变事件onhashchange,并且每次页面进来需要执行一次保持上一次的状态;改变点击时的样式,添加属性selected当为true时作用,即filterState会等于路由的时候,样式生效。
  1. data:{
  2. todos:todos,
  3. currentEditing:null,
  4. filterState:'all',
  5. },
  1. computed:{
  2. leftCount:function(){
  3. return this.todos.filter(item => !item.completed).length
  4. },
  5. filterTodos:function(){
  6. switch(this.filterState){
  7. case 'active':
  8. return this.todos.filter(item=>!item.completed);
  9. break;
  10. case 'completed':
  11. return this.todos.filter(item=>item.completed);
  12. break;
  13. default:
  14. return this.todos;
  15. break;
  16. };
  17. },
  1. <li v-for="(item,index) of filterTodos" v-bind:class='{completed:item.completed,editing:item===currentEditing}'>
  1. // 路由状态切换
  2. window.onhashchange=function(){
  3. var hash=window.location.hash.substr(2) || 'all';
  4. window.app.filterState=hash;
  5. };
  6. // 页面第一次进来,保持状态
  7. window.onhashchange();
  1. <ul class="filters">
  2. <li>
  3. <a :class="{selected:filterState==='all'}" href="#/">All</a>
  4. </li>
  5. <li>
  6. <a :class="{selected:filterState==='active'}" href="#/active">Active</a>
  7. </li>
  8. <li>
  9. <a :class="{selected:filterState==='completed'}" href="#/completed">Completed</a>
  10. </li>
  11. </ul>

三、项目完整代码和效果展示

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Template • TodoMVC</title>
  7. <link rel="stylesheet" href="node_modules/todomvc-common/base.css">
  8. <link rel="stylesheet" href="node_modules/todomvc-app-css/index.css">
  9. <!-- CSS overrides - remove if you don't need it -->
  10. <link rel="stylesheet" href="css/app.css">
  11. </head>
  12. <body>
  13. <!-- id="todoapp"vue管理模块入口 -->
  14. <section id="todoapp" class="todoapp">
  15. <header class="header">
  16. <h1>todos</h1>
  17. <input class="new-todo" placeholder="What needs to be done?" @keyup.enter='addTodo' v-focus>
  18. </header>
  19. <template v-if='todos.length'>
  20. <!-- This section should be hidden by default and shown when there are todos -->
  21. <section class="main">
  22. <!-- @click='toggleAll'点击事件 -->
  23. <input @click='toggleAll' id="toggle-all" class="toggle-all" type="checkbox" v-bind:checked='toggleState'>
  24. <label for="toggle-all">Mark all as complete</label>
  25. <ul class="todo-list">
  26. <!-- These are here just to show the structure of the list items -->
  27. <!-- List items should get the class `editing` when editing and `completed` when marked as completed -->
  28. <!-- vue列表渲染 -->
  29. <li v-for="(item,index) of filterTodos" v-bind:class='{completed:item.completed,editing:item===currentEditing}'>
  30. <div class="view">
  31. <input class="toggle" type="checkbox" v-model='item.completed'>
  32. <label @dblclick="currentEditing=item">{{item.title}}</label>
  33. <button class="destroy" @click='removeTodo(index,$event)' ></button>
  34. </div>
  35. <input class="edit" :value='item.title' @blur='saveEdit(item,index,$event)' @keyup.enter='saveEdit(item,index,$event)' @keyup.esc='currentEditing=null' v-editing-focus="item===currentEditing">
  36. </li>
  37. </ul>
  38. </section>
  39. <!-- This footer should hidden by default and shown when there are todos -->
  40. <footer class="footer">
  41. <!-- This should be `0 items left` by default -->
  42. <span class="todo-count"><strong>{{leftCount}}</strong> item left</span>
  43. <!-- Remove this if you don't implement routing -->
  44. <ul class="filters">
  45. <li>
  46. <a :class="{selected:filterState==='all'}" href="#/">All</a>
  47. </li>
  48. <li>
  49. <a :class="{selected:filterState==='active'}" href="#/active">Active</a>
  50. </li>
  51. <li>
  52. <a :class="{selected:filterState==='completed'}" href="#/completed">Completed</a>
  53. </li>
  54. </ul>
  55. <!-- Hidden if no completed items are left ↓ -->
  56. <button class="clear-completed" @click='removeAllDone'>Clear completed</button>
  57. </footer>
  58. </template>
  59. </section>
  60. <footer class="info">
  61. <p>Double-click to edit a todo</p>
  62. <!-- Remove the below line ↓ -->
  63. <p>Template by <a href="http://sindresorhus.com">Sindre Sorhus</a></p>
  64. <!-- Change this out with your name and url ↓ -->
  65. <p>Created by <a href="http://todomvc.com">you</a></p>
  66. <p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
  67. </footer>
  68. <!-- Scripts here. Don't remove ↓ -->
  69. <script src="node_modules/todomvc-common/base.js"></script>
  70. <script src="node_modules/vue/dist/vue.js"></script>
  71. <script src="js/app.js"></script>
  72. </body>
  73. </html>

index.html

  1. (function (Vue) {
  2. // 数据
  3. let todos=[
  4. {id:1,title:'睡觉',completed:true},
  5. {id:2,title:'美食',completed:false},
  6. {id:3,title:'代码',completed:true}
  7. ];
  8. // 全局自定义指令,自动获取焦点
  9. Vue.directive('focus', {
  10. inserted: function (el) {
  11. el.focus();
  12. }
  13. });
  14.  
  15. // vue实例
  16. window.app=new Vue({
  17. el:'#todoapp',
  18. data:{
  19. todos:todos,
  20. currentEditing:null,
  21. filterState:'all',
  22. toggleAllstate:true,
  23. },
  24. computed:{
  25. leftCount:function(){
  26. return this.todos.filter(item => !item.completed).length
  27. },
  28. filterTodos:function(){
  29. switch(this.filterState){
  30. case 'active':
  31. return this.todos.filter(item=>!item.completed);
  32. break;
  33. case 'completed':
  34. return this.todos.filter(item=>item.completed);
  35. break;
  36. default:
  37. return this.todos;
  38. break;
  39. };
  40. },
  41. // 全选的联动效果
  42. toggleState:function(){
  43. return this.todos.every(item=>item.completed);
  44. },
  45. },
  46. methods:{
  47. // 添加任务
  48. addTodo(event){
  49. let todoText=event.target.value.trim();
  50. if(!todoText.length){
  51. return
  52. }
  53. const lastTodo=this.todos[this.todos.length-1];
  54. const id=lastTodo?lastTodo.id+1:1;
  55. this.todos.push({
  56. id:id,
  57. title:todoText,
  58. completed:false,
  59. });
  60. event.target.value='';
  61. },
  62. // 点击全部完成或者未完成
  63. toggleAll(event){
  64. let checked=event.target.checked;
  65. this.todos.forEach(todo => todo.completed=checked);
  66. },
  67. // 删除单个任务项
  68. removeTodo(delIndex,event){
  69. this.todos.splice(delIndex,1);
  70. },
  71. // 显示所有未完成任务数(删除所有已完成)
  72. removeAllDone(){
  73. this.todos=this.todos.filter((item,index)=>{
  74. return !item.completed;//return true,即item.completed为false
  75. });
  76. },
  77. // 保存编辑项
  78. saveEdit(item,index,event){
  79. var editText=event.target.value.trim();
  80. // 如果为空,直接删除这个item
  81. if(!editText.length){
  82. return this.todos.splice(index,1);
  83. }
  84. // 如果不为空,修改title的值,然后去除eiditing样式
  85. item.title=editText;
  86. this.currentEditing=null;
  87. },
  88. },
  89. directives:{
  90. // 局部自定义属性
  91. editingFocus:{
  92. update(el,binding){
  93. if(binding.value){
  94. el.focus();
  95. }
  96. },
  97. },
  98. },
  99. });
  100. // 路由状态切换
  101. window.onhashchange=function(){
  102. var hash=window.location.hash.substr(2) || 'all';
  103. window.app.filterState=hash;
  104. };
  105. // 页面第一次进来,保持状态
  106. window.onhashchange();
  107. })(Vue);

app.js

框架入门经典项目TodoMVC的更多相关文章

  1. openjpa框架入门_项目框架搭建(二)

    Openjpa2.2+Mysql+Maven+Servlet+JSP 首先说明几点,让大家更清楚整体结构: 官方source code 下载:http://openjpa.apache.org/dow ...

  2. openjpa框架入门_项目 database 启动project 初始化(三)

    mysql数据库安装好,这里不多说,现在来执行sql脚本 http://download.csdn.net/detail/shenhonglei1234/6019677 将下载好的脚本后缀名“open ...

  3. openjpa框架入门_openbooks项目Overview(四)

  4. 『Scrapy』爬虫框架入门

    框架结构 引擎:处于中央位置协调工作的模块 spiders:生成需求url直接处理响应的单元 调度器:生成url队列(包括去重等) 下载器:直接和互联网打交道的单元 管道:持久化存储的单元 框架安装 ...

  5. Taurus.MVC 微服务框架 入门开发教程:项目集成:2、客户端:ASP.NET Core(C#)项目集成:应用中心。

    系列目录: 本系列分为项目集成.项目部署.架构演进三个方向,后续会根据情况调整文章目录. 本系列第一篇:Taurus.MVC V3.0.3 微服务开源框架发布:让.NET 架构在大并发的演进过程更简单 ...

  6. 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 目录索引

    索引 [无私分享:从入门到精通ASP.NET MVC]从0开始,一起搭框架.做项目(1)搭建MVC环境 注册区域 [无私分享:从入门到精通ASP.NET MVC]从0开始,一起搭框架.做项目(2)创建 ...

  7. ThinkJS框架入门详细教程(二)新手入门项目

    一.准备工作 参考前一篇:ThinkJS框架入门详细教程(一)开发环境 安装thinkJS命令 npm install -g think-cli 监测是否安装成功 thinkjs -v 二.创建项目 ...

  8. SpringMVC框架入门配置 IDEA下搭建Maven项目(zz)

    SpringMVC框架入门配置 IDEA下搭建Maven项目 这个不错哦 http://www.cnblogs.com/qixiaoyizhan/p/5819392.html

  9. Newbe.Claptrap 框架入门,第二步 —— 创建项目

    接上一篇 Newbe.Claptrap 框架入门,第一步 -- 开发环境准备 ,我们继续了解如何创建一个 Newbe.Claptrap 项目. Newbe.Claptrap 是一个用于轻松应对并发问题 ...

随机推荐

  1. HDU校赛 | 2019 Multi-University Training Contest 6

    2019 Multi-University Training Contest 6 http://acm.hdu.edu.cn/contests/contest_show.php?cid=853 100 ...

  2. Implementing Azure AD Single Sign-Out in ASP.NET Core(转载)

    Let's start with a scenario. Bob the user has logged in to your ASP.NET Core application through Azu ...

  3. VS 安装resharper 后 无法进行UnitTest

    Vs安装 Resharper后,无法进行单元测试,发现报错提示信息如下: ignored test-case is missing. rebuild the project and try again ...

  4. ASP.NET SignalR 系列(九)之源码与总结

    1.SignalR 1.0与2.0有些不同,以上篇章均只支持2.0+ 2.必须注意客户端调用服务端对象和方法时的大小写问题 3.客户端上的方法不能重名 4.IE7及以下的,需要增加json的分析器,分 ...

  5. Java之路---Day18(List集合)

    2019-11-05-23:03:28 List集合: java.util.List 接口继承自 Collection 接口,是单列集合的一个重要分支,习惯性地会将实现了List 接口的对象称为Lis ...

  6. vs2017 添加 mysql EF实体数据模型闪退

    1.查看vs2017安装路径找到Mysql.Data.dll版本号与MySQL Connector Net版本是否一致 历史版本下载地址 http://mysql.inspire.net.nz/Dow ...

  7. JavaScript之变量(声明、解析、作用域)

    声明(创建) JavaScript 变量 在 JavaScript 中创建变量通常称为"声明"变量. 一.我们使用 var 关键词来声明变量: var carname; 变量声明之 ...

  8. Active Directory渗透测试典型案例

    0x01 前言 我有几个客户在渗透测试之前来找我,说他们的系统安全做得非常好,因为他们的漏洞扫描显示没有严重的漏洞并且已准备好进行安全测试,这使我在15分钟内利用AD中的错误配置获得了域管理员权限. ...

  9. vm-install 模版创建虚拟机

    主要用到的信息有:模版id和存储id 通过存储名字 # xe vm-install template=[template_uuid] new-name-label="name" s ...

  10. Spark排序方式集锦

    一.简介 spark中的排序一般可以使用orderBy或sort算子,可以结合负号.ASC/DESC和col进行简单排序.二次排序等情况 二.代码实现 package big.data.analyse ...