用vue.js实现一个todolist项目:input输入框输入的值会呈现在下方,并且会保存在localStorage里面,而且下方的列表点击之后也会有变化:

完整代码:

App.vue

<template>
<div id="app">
<h1 v-html = "title"></h1>
<input v-model="newItem" v-on:keyup.enter="addNew" ></input>
<ul>
<li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">{{item.label}}</li>
</ul>
</div>
</template> <script>
import Store from './store'
export default {
data:function(){
return {
title:"This Is A Todolist",
items:Store.fetch(),
newItem:""
}
},
watch:{
items:{
handler:function(items){
Store.save(items)
},
deep:true
}
},
methods:{
toggleFinish:function(item){
item.isFinished = !item.isFinished
},
addNew:function(){
this.items.push({
label:this.newItem,
"isFinished":false
})
this.newItem=""
}
}
}
</script> <style>
.finished{
text-decoration:underline;
}
li{
list-style:none;
font-size:.6em;
margin-top:10px;
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
input{
width:230px;
height:40px;
border-radius:20px;
padding: .4em .35em;
border:3px solid #CFCFCF;
font-size: .55em;
}
</style>

store.js:

const STORAGE_KEY='todos-vuejs'
export default {
fetch:function(){
return JSON.parse(window.localStorage.getItem(STORAGE_KEY)||'[]');
},
save:function(items){
window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items))
}
}

详细解析

ES6的写法:

export default {
name: 'hello',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}

export default 和 export 区别:

  1.export与export default均可用于导出常量、函数、文件、模块等
  2.你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使用
  3.在一个文件或模块中,export、import可以有多个,export default仅有一个
  4.通过export方式导出,在导入时要加{ },export default则不需要

.export
//demo1.js
export const str = 'hello world'
export function f(a){ return a+}
对应的导入方式: //demo2.js
import { str, f } from 'demo1' //也可以分开写两次,导入的时候带花括号 .export default
//demo1.js
export default const str = 'hello world'
对应的导入方式: //demo2.js
import str from 'demo1' //导入的时候没有花括号

当最简单导入的时候,这个值是将被认为是”入口”导出值。

在App.vue中完成项目编写:

组件布局将在这里设置,.vue文件将由vue-loader进行加载,.vue内同时包含html、css、js源码,使组件的独立,组件之间可以尽可能地解耦,便于开发维护

先看一个简单示例:只要isFinished为true就加下划线,false就不加下划线:

<template>
<div id="app">
<h1 v-html = "title"></h1>
<ul>
<li v-for="item in items" v-bind:class="{finished:item.isFinished}">{{item.label}}</li>
</ul>
</div>
</template> <script>
import Hello from './components/Hello' export default {
data:function(){
return {
title:"this is a todolist",
items:[
{
label:"coding",
"isFinished":false
},
{
label:"walking",
"isFinished":true
}
]
}
}
}
</script> <style>
.finished{
text-decoration:underline;
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

对于class的控制如上:如果是数组的话则可以渲染多个。

再进一步完成功能:点击没有下划线的li就会加下划线,有下划线就会去除下划线。

需要绑定事件:

<li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">{{item.label}}</li>

还要添加方法toggleFinish():

  methods:{
toggleFinish:function(item){
item.isFinished = !item.isFinished
}
}

将input输入的值添加到列表下面

添加input:

<input v-model="newItem" v-on:keyup.enter="addNew" ></input>

data对象添加:

newItem:""

添加方法:

//addNew:function(){
// alert(this.newItem)
// this.newItem="" //添加后加输入框清空
//} addNew:function(){
this.items.push({
label:this.newItem,
"isFinished":false
})
this.newItem=""
}

使用localStorage来存储

使用store.js:

const STORAGE_KEY='todos-vuejs'
export default {
fetch:function(){
return JSON.parse(window.localStorage.getItem(STORAGE_KEY)||'[]');
},
save:function(items){
window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items))
}
}

两个方法:一个设置,一个获取

导入:

import Store from './store'

打印一下Store,console.log(Store),可以看到:

由于加入代码中每次都需要添加还有删除等等,如果每次都用到store的方法,这就有点麻烦了,所以这里就要用到watch观察。

  watch:{
items:{
handler:function(val,oldVal){
console.log(val,oldVal)
},
deep:true
}
},

可以看到打印出:

使用save()方法:

  watch:{
items:{
handler:function(items){
Store.save(items)
},
deep:true
}
},

一有变化就会触发。

将fetch()方法也加进去:

<script>
import Store from './store'
export default {
data:function(){
return {
title:"<span>?</span>this is a todolist",
items:Store.fetch(),
newItem:""
}
},
watch:{
items:{
handler:function(items){
Store.save(items)
},
deep:true
}
},
methods:{
toggleFinish:function(item){
item.isFinished = !item.isFinished
},
addNew:function(){
this.items.push({
label:this.newItem,
"isFinished":false
})
this.newItem=""
}
}
}
</script>

Vue.js计算属性和样式       --2017.03.07

用vuejs实现一个todolist项目的更多相关文章

  1. 使用vuejs做一个todolist

    在输入框内输入一个list,回车,添加到list列表中,点击列表中的项样式改变 1.index.html <!DOCTYPE html> <html> <head> ...

  2. Vue.js基础篇实战--一个ToDoList小应用

    距离开始学Vue已经过去一个多月了,总想把学到的东西柔和在一起,做点东西出来,于是有了这个Todolist小应用. 使用vuex 纯粹基础,没有用到web pack,vuex,npm,下次把它改造一下 ...

  3. TODOList项目

    [ 爱上Swift]十二期:TODOList项目   好久没有写Swift甚是想念,Swift,Xcode都比较稳定了写个程序熟悉一下,当然时间原因都是小Demo,废话不多说先上图. 下面是跑起来之后 ...

  4. Vuejs实例-02Vue.js项目集成ElementUI

    Vuejs实例-02Vue.js项目集成ElementUI 0:前言 vue.js的UI组件库,在git上有多个项目,我见的使用者比较多的是iView和Element.两个组件库,组件都很丰富. 官网 ...

  5. 利用 vue-cli 构建一个 Vue 项目

    一.项目初始构建 现在如果要构建一个 Vue 的项目,最方便的方式,莫过于使用官方的 vue-cli . 首先,咱们先来全局安装 vue-cli ,打开命令行工具,输入以下命令: $ npm inst ...

  6. VUE系列一:VUE入门:搭建脚手架CLI(新建自己的一个VUE项目)

    一.VUE脚手架介绍 官方说明:Vue 提供了一个官方的 CLI,为单页面应用快速搭建 (SPA) 繁杂的脚手架.它为现代前端工作流提供了 batteries-included 的构建设置.只需要几分 ...

  7. [FE] 有效开展一个前端项目2 (vuejs-templates/webpack)

      1.安装 nodejs.npm $ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - $ sudo apt-get i ...

  8. 开发“todolist“”项目及其自己的感悟

    一,项目题目: 实现“todolist项目” 该项目主要可以练习js操控dom事件,事件触发之间的逻辑关系,以及如何写入缓存,获取缓存.固定. 二,todolist简介 ToDoList是一款非常优秀 ...

  9. 如何搭建一个vue项目(完整步骤)

    参考资料 一.安装node环境 1.下载地址为:https://nodejs.org/en/ 2.检查是否安装成功:如果输出版本号,说明我们安装node环境成功 3.为了提高我们的效率,可以使用淘宝的 ...

随机推荐

  1. 代理延迟加载中proxy和弄no-proxy区别

    Child   <-   many-to-one   ->Parent         class   Child   {         private   Parent   paren ...

  2. 第4章 文本编辑器vim

    1. vim常用操作 1.1 vim简介 (1)vim是一个功能强大的全屏幕文本编辑器,是Linux/Unix上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件. (2)vim没有菜单,只有命令 ...

  3. 前端MVC学习总结——AngularJS验证、过滤器

    前端MVC学习总结--AngularJS验证.过滤器 目录 一.验证 二.过滤器 2.1.内置过滤器 2.1.1.在模板中使用过滤器 2.1.2.在脚本中调用过滤函数 2.2.自定义过滤器 三.指令( ...

  4. jdbc java数据库连接 1)jdbc入门

      之前操作数据 1)通过mysql的客户端工具,登录数据库服务器  (mysql -u root -p 密码) 2)编写sql语句 3)发送sql语句到数据库服务器执行 什么是jdbc? 使用jav ...

  5. Mobile Safari调用本地App, 否则进入App Store下载

    需求: 如何让用户通过手机浏览器(Mobile Safari),访问一个URL就能直接打开ios上的App应用,如果该应用没有安装,那么直接跳转到App Store的App下载页面. 准备工作 ios ...

  6. 编写、部署、应用JavaBean

    编写javabean   编写javabean实质上就是编写一个java类.设计javabean类就是要设计这个javabean的属性和方法,类的方法的命名遵循以下规则: 1)如果成员变量的名字是xx ...

  7. 使用Unity Container

    Lab1.使用Unity Container Unity Container最主要的两个方法就是RegisterType和Resolve了,RegisterType用于注册类型的映射,而Resolve ...

  8. caffe中accuracy和loss用python从log日志里面获取

    import re import pylab as pl import numpy as np if __name__=="__main__": accuracys=[] loss ...

  9. requestAnimationFrame,Web中写动画的另一种选择

    HTML5/CSS3时代,我们要在web里做动画选择其实已经很多了: 你可以用CSS3的animattion+keyframes; 你也可以用css3的transition; 你还可以用通过在canv ...

  10. HTML+CSS知识点总结

    转自:http://blog.csdn.net/qiushi_1990/article/details/40260447?utm_source=tuicool&utm_medium=refer ...