1. 程式人生 > >初學Vue練手的簡易todolist

初學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>