ES6项目小练习TodoList15
ES6技术本身非常容易,相信大家也体会到了。各种新特性都不难,但是为什么很多人学习起来困难呢? 其实ES6难的不是技术,而是实际工作环境的搭建。比如我们想写一个简单的ES6版本的TodoList. 很多同学学生是这么放弃的: 通过搜索引擎,发现很多教程都是直接引入Traceur.js 然后讲解ES6各种功能和语法的,但是好像实际并不是直接引入Traceur.js ,而是用babel。 使用babel的话好像需要安装node.js,还有会用npm 安装好npm 以后我是该使用gulp还是webpack呢? 嗯,应该webpack吧,这个好像目前更主流。好,就选它了。 webpack怎么安装?是不是需要webpack-cli?另外webpack 4.0好像更好用,但是好像安装又有兼容性问题? 纠结ing…… 考虑了半天后,终于下定决心,就用最新版本,学老的没用。 安装好webpack 4.0,对了是不是要配置工作流? 对配置吧,这才是“工作的方式”,js需要压缩,装个压缩插件,webpack怎么用啊?有各种rule,plugins,还有entry. 开始头疼,耐着性子把网上的教程配置完,这个插件怎么报错了啊? 继续查,原来是webpack 4.0和它不兼容,换webpack 3.0还是换插件? 纠结半天后,终于鼓起勇气,换插件! 又配置出错了…… 开始进入暴走模式,又气又恼。 好吧,折腾半天,请教大牛总算通过了。 那么问题来了,学了这么久css和js,我居然不知道往哪里写css…… 好吧,听说得用sass…… 配置完了,sass语法不会…… 我就想写一个ES6的todoList,太费劲了,咋得学这么东西啊…… 还是放弃吧,我感觉我不适合做前端。 虽然夸张了些,但是大部分前端都有类似的经历。 今天我就让大家彻底的学会如何工作中写ES6,我们依然用todoList举例,对了我们并不需要学习webpack,sass,插件等等。我们只学习ES6,对其它的统统不用学,你会用就可以,也不过几个命令而已。 ok,我们开始。 首先,拿到我配置好的工作流,直接在根目录下进入命令行,然后 npm install安装完成后,使用 npm run dev然后就可以用了, 就这几个文件,对应写html,js和css。 首先我们先写 html文件 。 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"> <title>TODOList</title></head><body> <div class="container"> <div class="item-area"> <h1 class="title">TODO-LIST</h1> <ul class="item"> <li class="item-li-default">输入任务...</li> </ul> </div> <div class="input-area"> <form class="add-item"> <input maxlength="15" type="text" name="item" placeholder="待办事件..." class="add-text" required> <input type="submit" value="+ 添加" class="add-button"> </form> </div> </div></body></html>接着,写css美化一下 * { padding: 0; margin: 0; } li { list-style: none; } html { display: flex; justify-content: center; align-items: center; text-align: center; min-height: 100vh; box-sizing: border-box; font-family: Futura, "Trebuchet MS", Arial, sans-serif; background: #ffc600; } svg { fill: #fff; background: rgba(0,0,0,0.1); padding: 20px; border-radius: 50%; width: 100px; margin-bottom: 50px; } .container { padding: 20px; max-width: 350px; box-shadow: 0 0 0 10px rgba(0,0,0,0.1); background: #fff; } .container .item-area .title { text-align: center; margin: 10px 0; color: #aaa; font-weight: 200; } .container .item-area .item { background: #fff; border-radius: 4px; } .container .item-area .item .item-li-default { color: #46b7b9; } .container .item-area .item .item-li { width: 100%; border-bottom: 1px solid #eee; height: 30px; line-height: 30px; text-align: left; color: #f95959; } .container .item-area .item .item-li .delete-item { float: right; outline: none; width: 44px; height: 26px; border-radius: 4px; color: #f05941; background: #fafafa; } .container .item-area .item .item-li input:checked + .item-text { color: #196b69; text-decoration: line-through; } .container .input-area { padding-top: 10px; } .container .input-area .add-text { outline: 0; border: 1px solid rgba(0,0,0,0.1); height: 40px; border-radius: 4px; text-indent: 10px; } .container .input-area .add-button { outline: 0; height: 40px; border-radius: 4px; width: 60px; padding: 4px; background-color: #fff; border: 1px solid rgba(0,0,0,0.1); }ok,使用 ...