568数据 568数据


vue.js todolist实现代码

网络编程 vue.js todolist实现代码 06-22

案例知识点:

1.vue.js基础知识

2.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>
 <div 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='toogleFinish(item)'>
  {{item.label}}
  </li>
 </ul>
 </div>
</template>
<script>
import Store from './store'
export default {
 name: 'app',
 data () {
 return {
  title: 'this is a todo list',
  items:Store.fetch(),
  newItem:''
 }
 },
 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 = ''
 }
 }
}
</script>
<style>
#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;
}
.finished{
 text-decoration: underline;
}
</style>

查看图片

总结

以上所述是小编给大家介绍的vue.js todolist实现代码,希望对的大家有所帮助!

vue.js 父向子组件传参的实例代码
1.新建componentA.vue组件,代码如下:store.js代码如下:constSTORAGE_KEY='todos-vue.js'exportdefault{fetch(){returnJSON.parse(window.localStorage.getItem(STORAGE_KEY)||'[]')},save(items){

Vue.js划分组件的方法
常见的一些页面,大家坐在一起敲代码就可以了,做完这个页面再做别的页面,但是作为一个功能复杂的系统,尤其是使用一些适合模块化开发的框架

vue中的scope使用详解
我们都知道vueslot插槽可以传递任何属性或html元素,但是在调用组件的页面中我们可以使用templatescope="props"来获取插槽上的属性值,获取到的值是一个对


编辑:568数据

标签:代码,插槽,组件,页面,是一个