狂神老师用的变量名 不容易区分,我改了一下更好理解一些。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="./vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
</head>
<body>
<div id="app">
<todo>
<todo-title slot="todo-title" :title="标题"></todo-title>
<todo-items slot="todo-items" v-for="(元素,下标) in todoItems"
:item="元素" :index="下标"
v-on:remove事件="removeItems方法(下标)"></todo-items>
</todo>
</div>
<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="remove">删除</button></li> ',
methods:{
remove:function (下标){
alert("删除触发");
this.$emit('remove事件',下标);
}
}
});
var vm=new Vue({
el: "#app",
data: {
标题: "列表",
message: "hello,zxy",
todoItems:['ks','zxy','bbbb'],
},
methods: {
removeItems方法:function (index){
console.log("删除了"+this.todoItems[index]+"元素成功!");
this.todoItems.splice(index,1);
}
}
});
</script>
</body>
</html>