vue.js,基础用法实例
2017-06-30 17:16
746 查看
vue的基础用法实例
v-if , v-elsev-on ( 缩写 @ )
v-bind ( 缩写 : )
v-for
v-model
methods
el
watch
data
demo可直接复制使用,自行下载vue.js文件,助于熟悉基础
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>to-do-list</title> <style type="text/css"> #vuedotolist{width: 400px;height: 500px;border: 2px solid #333;border-radius: 3px;margin:60px auto;position: relative;} .edit{width: 80%;height: 35px;line-height: 35px;margin-top:30px;box-shadow: 0 0 4px #ccc;border: 1px solid #ccc;border-radius: 4px;outline: 0;box-sizing: border-box;margin-left: 10%;text-indent: 20px;} .list{margin:0 auto;max-width: 80%;height: 60%;overflow-y: auto;} .unit{position: relative;padding: 10px 0;border-bottom: 1px solid #efefef;} .unit:last-child{border-bottom: 0;} .finish{text-decoration: line-through;color: #ccc;} .del{background: red;text-decoration: none;position: absolute;right: 20px;font-size: 12px;border:0;padding: 2px 5px;border-radius: 5px;color: #fff;cursor:pointer;} .empty{font-size: 14px;color: #928787;text-align: center;padding: 10px 0;} .number{position: absolute;right: 40px;bottom: 0px;} </style> </head> <body> <div id="vuedotolist"> <input v-model="task.content" type="text" class="edit" placeholder="编写任务" @keydown.enter="addTask" /> <p v-if="list.length==0" class="empty">今天暂无安排...</p> <div class="list" v-else> <div class="unit" v-for="(item,index) in list"> <input type="checkbox" :checked="item.finished" @click="changeState(index)" /> <span :class="{'finish':item.finished}">{{item.content}}</span> <button @click="removeTask(index)" class="del">删除</button> </div> </div> <p class="number">任务数量:{{list.length}}</p> </div> </body> <!-- <script src="https://unpkg.com/vue/dist/vue.js"></script> --> <script src="src/vue.js"></script> <script> const STORAGE_KEY = 'to-do-list' var fetch=function(){ return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]') }; var save=function(items){ window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items)) }; const vuedotolist = new Vue({ el:"#vuedotolist", data:{ //默认 task:{ content:'', //内容 finished:false, //未完成 }, //任务列表,默认取localStorage的数据 list:fetch() }, methods:{ //添加新的任务 addTask:function(){ if (this.task.content != "") { this.list.push(this.task); // save(this.task); //存入list之后,重置task this.task={ content:'', finished:false, // deleted:false } } }, //取反值,修改状态 changeState:function(index){ let curSate=this.list[index].finished; this.list[index].finished=!curSate; }, //移除当前的数据,重新返回数组给lsit removeTask:function(index){ this.list.splice(index,1); } }, //监听,一旦list[]发生变化,存入localStorage数据 watch:{ list:{ handler:function(){ save(this.list); } } } }) </script> </html>
相关文章推荐
- Vue.js自定义指令的用法与实例
- vue.js基础-属性与方法和实例生命周期
- Vue.js自定义指令的用法与实例解析
- Vue.js基础指令实例讲解(各种数据绑定、表单渲染大总结)
- vue.js计算属性computed用法实例分析
- vue.js计算属性用法(computed)技巧,依赖其他vue实例的数据
- vue.js中toast用法及使用toast弹框的实例代码
- js正则标志/g /i /m的用法,以及实例
- js一些基础用法
- .net,c#环境下,Datalist和Repeater的基础用法实例
- js中arguments的用法(实例讲解)
- Node.js基础入门实例讲解
- 【javascript基础】JS正则表达式的实例属性
- js createRange与createTextRange的一些用法实例
- Jquery基础_弹出对话框_js的onload_jq的多次加载_Js和JQ对象转换_实例(一)
- js正则标志/g /i /m的用法,以及实例
- js基础用法全集
- js基础:window.moveTo()函数用法示例
- 【javascript基础】JS正则表达式的实例方法
- js split 的用法和定义 js split分割字符串成数组的实例代码