天天看點

todolist元件(局部元件/全局元件/删除功能)

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>TodoList元件的删除功能.html</title>

<script type="text/javascript" src="./vue.js" ></script>

</head>

<body>

<!--TodoList元件的删除功能.html-->

<div id="root">

<div>

      <input v-model="inputValue"/>

<button @click="handleSubmit">送出</button>

</div>

<ul>

<!--todolist元件-->

<!--<li v-for="(item, index) of list" :key="index">

{{item}}

</li>-->

<!--全局定義-->

            <todo-item 

            v-for="(item, index) of list" 

            :key="index"

            :content="item"

            :index="index"

@delete="handleDelete"

            >

            </todo-item>

</ul>

</div>

<script>

//todolist元件 全局定義

Vue.component('todo-item',{

props:['content' ,'index'],

//每一個veu執行個體 都是一個元件  每個元件都是一個vue 執行個體

template : '<li @click="handleClick">{{content}}</li>',

methods:{

handleClick:function(){

                 this.$emit('delete', this.index)

},

}

})

        //局部元件

//       var TodoItem = {

//        template:'<li>item</li>'

//       }

new Vue({

el:"#root",

// //如果你想在其他veu執行個體裡邊用這個局部元件 必選通過components 對這個局部元件進行注冊

// components:{

// //通過todo-item這個标簽來使用

// 'todo-item':TodoItem  

// },

      data:{

      inputValue:'',

      list:[]

      },

      methods:{

      handleSubmit:function(){

      this.list.push(this.inputValue),

      this.inputValue=''// 清空inputValue内容

      },

      handleDelete:function(index){

this.list.splice(index, 1)

}

      }

})

</script>

</body>

</html>