首页  >  文章  >  web前端  >  vue.js todolist如何实现

vue.js todolist如何实现

php中世界最好的语言
php中世界最好的语言原创
2017-12-31 11:36:112515浏览

这次给大家带来的是vue.js todolist如何实现,首先我们要知道vue.js基础知识和HTML5 本地存储localstorage,这篇文章就给大家好好分析一下。

store.js代码

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

App.vue代码


<template>
 <p id="app">
 <h1 v-text="title"></h1>
 <input v-model="newItem" v-on:keyup.enter="addNew"/>
 <ul>
  <li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click=&#39;toogleFinish(item)&#39;>
  {{item.label}}
  </li>
 </ul>
 </p>
</template>
<script>
import Store from &#39;./store&#39;
export default {
 name: &#39;app&#39;,
 data () {
 return {
  title: &#39;this is a todo list&#39;,
  items:Store.fetch(),
  newItem:&#39;&#39;
 }
 },
 watch:{
  items:{
  handler(items){  //经过变化的数组会作为第一个参数传入
   Store.save(items)
   console.log(Store.fetch());
  },
  deep:true  //深度复制
  }
 },
 methods:{
 toogleFinish(item){
  item.isFinished = !item.isFinished
 },
 addNew(){
  this.items.push({
  label:this.newItem,
  isFinished:false,
  })
  this.newItem = &#39;&#39;
 }
 }
}
</script>
<style>
#app {
 font-family: &#39;Avenir&#39;, Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
.finished{
 text-decoration: underline;
}
</style>


相信看了以上介绍你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

相关阅读:

ES6解构赋值的功能如何使用

js实现仿window系统日历效果

JS怎样可以做到点击跳转到登陆的个人邮箱

以上是vue.js todolist如何实现的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn