<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
<script src="js/axios.js"></script>
</head>
<body>
<div id="app">
<todo>
<todo-title slot="todo-title" v-bind:title=title></todo-title>
<todo-items slot="todo-items" v-for="(item,index) in itemList"
v-bind:index="index" v-bind:item="item"
v-on:laowangdelete="vueDelete(index)"></todo-items>
</todo>
</div>
</body>
</html>
<script>
Vue.component("todo", {
template: "<div> <slot name='todo-title'></slot>" + "<ul><slot name='todo-items'></slot> </ul></div>"
})
Vue.component("todo-title", {
props: ['title'],
template: " <div>{{title}}</div>"
})
Vue.component("todo-items", {
props: ['item', 'index'],
template: "<li>{{index}}.{{item}} <button @click='ButtonRemove(index)'>删除</button></li>",
methods: {
ButtonRemove: function (index) {
//注意laowangdelete是自定义事件名称,小写
//通过绑定的自定义事件(laowangdelete)来调用vue事例中的函数
this.$emit('laowangdelete', index)
}
}
})
new Vue({
el: "#app",
data: {
title: "来自Vue事例的待办事项title",
itemList: ['读书', '学习', '运动']
},
methods: {
vueDelete: function (index) {
//splice 从当前元素删除1个
this.itemList.splice(index,1)
}
}
})
</script>

