<!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>
<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>