用 vue 快速做一个简单点的 TODO-LIST

思路整理

主要涉及知识点:

  1. vue 如何创建并运行项目 vue create xxx;
  2. 如何定义data() { return {} }及渲染数据 {{stu}};
  3. 如何定义移除/增添函数; 添加form表单; 提交数据;
  4. filter() 方法的使用来遍历并过滤数组;
  5. 如何阻止函数的默认行为: e.preventDefault() ;
  6. 如何拷贝对象 const stu = Object.asign({}, this.stu) ;
  7. push() 方法的使用, 为数组添加元素
  8. 清空表单

代码块

<template>
  <div id="app">
    <form action="">
      <input type="text" v-model="stu.id">
      <input type="text" v-model="stu.name">
      <input type="text" v-model="stu.age">
      <input type="submit" value="确定" @click="addStu">
    </form>
    <ul>
      <li v-for="(stu, index) in stus" :key = "stu.id" @click="remStu(index)">
        {{stu.id}} ---- {{stu.name}} ---- {{stu.age}}
      </li>
    </ul>
  </div>
</template>

<script>

export default {
  name: 'App',
 data() {
   return {
    stus: [
      {id: 1, name: 'sonia', age: 12},
      {id: 2, name: 'bolin', age: 22},
      {id: 3, name: 'alice', age: 32}
    ],
    stu: {
      id: '',
      name: '',
      age: ''
    }
   }
 },
 methods: {
   // 移除
   remStu(index) {
     // 如果点击的 idx 与 当前  list 的 index 一样,则筛选掉, 否则保留
     // filter()过滤后组成的数组 赋值给 stus
     this.stus = this.stus.filter((stu, idx)=>idx !== index)
   },
   // 增加
   addStu(e) {
     // 阻止默认行为
     e.preventDefault()
     // 拷贝对象
     const stu = Object.assign({}, this.stu)
     // 推送到 stus 数组里
     this.stus.push(stu)
     this.stu.id = '',
     this.stu.name = '',
     this.stu.age = ''
   }
 }
}
</script>

<style>

</style>

后记

按思路走下来,感觉很顺,掌握 vue2 就能很轻松的写出一个 TODO-list 样式按自己喜好,重点是掌握知识点用法!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值