基于vuejs实现一个todolist项目

所属分类: 网络编程 / JavaScript 阅读数: 1032
收藏 0 赞 0 分享

用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:1.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: 0.4em 0.35em;
 border:3px solid #CFCFCF;
 font-size: 1.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则不需要

1.export

//demo1.js
export const str = 'hello world'
export function f(a){ return a+1}
对应的导入方式:

//demo2.js
import { str, f } from 'demo1' //也可以分开写两次,导入的时候带花括号

2.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>


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

setInterval和setTimeout停止的方法

要想知道它们是怎么停止的,首先我们要了解它们的运行机制和原理,下面是具体的介绍。
收藏 0 赞 0 分享

使用JavaScript让网页的title动起来

使用webQQ有些时间了,webQQ每次收到信息,就会看到title提示那个网友或群来信息,发现挺有意思,其实这个很简单。
收藏 0 赞 0 分享

如jQuery般易用的api风格代码分享

最近几天闲来无事,随便写了点代码玩了玩。个人觉得,应该把编码当做一种乐趣,要不然会觉得很累...
收藏 0 赞 0 分享

js contains方法实现代码

IE有许多好用的方法,后来都被其他浏览器抄袭了,比如这个contains方法。如果A元素包含B元素,则返回true,否则false。唯一不支持这个方法的是IE的死对头firefox。
收藏 0 赞 0 分享

禁止JS运行的代码

如果我们要让页面上的javascript不运行,方法最简单的就是使用noscript标签,经常被用来屏蔽那些免费空间要加上的JS。
收藏 0 赞 0 分享

javascript提取内容到作为文章简介的代码

javascript提取文本框内容到另一个文本框,经常被用来做内容简介的读取,这样编辑就不用每次都去处理一篇文章的简介了。
收藏 0 赞 0 分享

javascript实现网站顶部出现几秒后图片缓慢消失的效果

快过年了,很多网站首页都放上了新春的祝福,有些是直接换成皮肤了,而有些则是用一张很大的图片放在网站顶部,在倒数几秒后,缓慢升上去直到最后消失。
收藏 0 赞 0 分享

javascript代码加载优化方法

给你的网站加上代码统计!常用的方法是直接加统计代码到网页,但你的网页数量很多呢?
收藏 0 赞 0 分享

jquery键盘事件介绍

jquery键盘事件介绍,使用jquery的朋友可以参考下。
收藏 0 赞 0 分享

基于jQuery实现表格数据的动态添加与统计的代码

使用jQuery可以大大减轻工作量,在实际开发中,使用了jQuery的clone(true)函数,该函数可以创建一个jQury对象的副本,并且参数为true时,可以复制该元素的所有事件处理函数。
收藏 0 赞 0 分享
查看更多