vue todolist

最近初学vue,做最简单的todolist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>todolist</title>
        <style type="text/css">
            #container{
                width: 700px;
                height: 100px;
                padding: 40px;
                margin: 0 auto;
            }
            li{
                list-style: none;
            }
            .close-btn{
                display: inline-block;
                width: 20px;
                height: 20px;
                background: url('close.png') no-repeat;
                cursor: pointer;
            }
            .finished{
                text-decoration: line-through;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <input type="text" v-model="newitem" @keyup.enter="addlistitem"/>
            <div class="todo-list">
                <ul>
                    <li v-for="(listitem,index) in list">
                        <input type="checkbox" v-model="listitem.isFinished" />
                        <span v-bind:class="{ finished: listitem.isFinished }" >{{ listitem.text }}</span>
                        <span class="close-btn" @click="deleteitem(index)"></span>
                    </li>
                </ul>
            </div>
        </div>
         
         
        <script src="https://cdn.jsdelivr.net/npm/vue"></script>
        <script>
            var app = new Vue({
              el: '#container',
              data: {
                newitem:'',
                list:[]
              },
              watch: {
                // 如果 `list`数据 发生改变,这个函数就会运行
                list: {
                    handler:function () {
                        this.saveTolocal(this.list)
                    },
                    // 深度观察,当对象里的属性发生改变时,也会触发watch。点击checkbox需要deep: true,否则watch不会起作用。
                    deep: true
                }
              },
              methods:{
                // 添加项目
                addlistitem:function(){
                    if(this.newitem != ''){
                        this.list.push({'text':this.newitem,'isFinished':false})
                        this.newitem = ''
                    }
                },
                // 删除项目
                deleteitem:function(curIndex){
                    this.list.splice(curIndex,1)
                },
                // 存入localStorage
                saveTolocal:function(data){
                    localStorage.setItem('tododata',JSON.stringify(data))
                }
              }
            });
             
            // 读取localStorage
            if(!!localStorage.getItem('tododata')){
                app.list = JSON.parse(localStorage.getItem('tododata'))
            }
             
        </script>
         
         
    </body>
</html>

  

posted @   说中  阅读(352)  评论(1编辑  收藏  举报
编辑推荐:
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
阅读排行:
· DeepSeek V3 两周使用总结
· 回顾我的软件开发经历(1)
· C#使用yield关键字提升迭代性能与效率
· 低成本高可用方案!Linux系统下SQL Server数据库镜像配置全流程详解
· 4. 使用sql查询excel内容
点击右上角即可分享
微信分享提示