Vue 实现基本的todoList教程

  这篇博客记录 vue 的第一个 demo todoList 的实现过程。

  1.测试, 引入 vue 确保可以取到数据

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>todoList</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  hello {{test}}
</div>

<script>
new Vue({
  el: '#app',
  data: {
    test: 'world!'
  }
})
</script>
</body>
</html>

界面显示值:hello world

2.编辑HTML 实现界面

<div id="addItem">
  <input type="text" v-model="newItem"/>
  <button v-on:click="addNewItem()">add</button>
    <ol>
        <li v-for="(item,index) in inputData">
            {{item}}
            <button v-on:click="dele(index)">dele</button>
        </li>
    </ol>
</div>

3. 添加JS事件

<script>
var addItem= new Vue({
    el: '#addItem',
    data: {
        inputData: [],
        newItem: ''
    },
    methods: {
        addNewItem: function() {
            this.inputData.push(this.newItem);
            this.newItem="" ;
        },
        dele:function(index){
          this.inputData.splice(index,1)
        }
     }
});
</script>

4. 扩展新功能,添加已完成与未完成分类功能

HTML:

<div id="addItem">
  <input type="text" v-model="newItem"/>
  <button v-on:click="addNewItem()">add</button>
    <ol>
      <div>待完成:{{inputData.length}}</div>
        <li v-for="(item,index) in inputData">
            {{item}}
            <button v-on:click="dele(index)">dele</button>
            <button v-on:click="complete(index)">complete</button>
        </li>
        <div>已完成:{{comleteItem.length}}</div>
            <li v-for="(item,index) in completeItem">
                <del>{{item}}</del>
                <button v-on:click="deleComplete(index)">dele</button>
        </li>
    </ol>
</div>

JAVASCRIPT:

<script>
var addItem= new Vue({
    el: '#addItem',
    data: {
        inputData: [],
        completeItem:[],
        newItem: ''
    },
    methods: {
        addNewItem: function() {
            this.inputData.push(this.newItem);
            this.newItem="" ;
        },
        dele:function(index){
             this.inputData.splice(index,1)
        },
      complete:function(){
            var comItem=this.inputData[index];
            this.inputData.splice(index,1);
            this.completeItem.push(comItem);
        }
        deleComplete:function(){
         this.completeItem.splice(index,1);
       }
    }
    }
});
</script>






  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值