美文网首页
vue实现简单的todoList

vue实现简单的todoList

作者: jh2k15 | 来源:发表于2018-05-03 11:37 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="root">
        <input v-model="inputValue" type="text">
        <button  @click="addList">add</button>
        <ul>
            <todo-item 
                v-for="(item,index) of list"
                :key="index"
                :content="item"
                :index="index"
                @delete="deleteHandle"
            />
        </ul>
    </div>
    <!-- vue -->
    <script>
        // 全局定义
        Vue.component('todo-item',{
            props:['content','index'],
            template:'<li @click="deleteItem">{{content}}</li>',
            methods:{
                deleteItem(){
                    this.$emit('delete',this.index)
                }
            }
        })

        //局部定义
        // let todoItem={
        //     template:'<li>{{item}}</li>'
        // }
        new Vue({
            el:'#root',
            // components:{
            //     'todo-item':todoItem
            // },
            data:{
                inputValue:"",
                list:[]
            },
            methods:{
                addList(){
                    this.list.push(this.inputValue)
                    this.inputValue=''
                },
                deleteHandle(idx){
                    this.list.splice(idx,1);
                }
            }
        })
    </script>
</body>
</html>

相关文章

网友评论

      本文标题:vue实现简单的todoList

      本文链接:https://www.haomeiwen.com/subject/crykrftx.html