初学Vue练手的简易todolist

此简易Demo就不写样式了,主要是熟悉Vue的指令


功能:添加、删除待办事项
这里写图片描述

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>todolist</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="root">
        <h2>简易todolistDemo</h2>
        <input type="text" v-model="inputValue"/>
        <button @click="handleSubmit">提交</button>
        <p>待办事项:</p>
        <ul>
            <todo-item v-for="(item,index) in list"
                :key="index"
                :content="item"
                :index="index"
                @delete="handleDelete">
            </todo-item>
        </ul>

    </div>
<script>

    Vue.component('todo-item',{
        props:['content','index'],
        template:'<li @click="handleClick">{{content}}</li>',
        methods:{
            handleClick:function(){
                this.$emit('delete',this.index);
            }
        }
    });

    new Vue({
        el:"#root",
        data:{
            inputValue:'',
            list:[]
        },
        methods:{
            handleSubmit:function(){
                this.list.push(this.inputValue);
                this.inputValue = '';
            },
            handleDelete:function(index){
                this.list.splice(index,1);
            }
        }
    });
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/lxjstudyit/article/details/80329133
今日推荐