qq_且行且珍惜_32
2019-10-31 17:17
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue中组件与实例的关系</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="root">
<div>
<input v-model="inputValue" />
<button @click="handleSubmit">提交</button>
</div>
<ul>
<todo-item v-for="(item,index) of list"
:key="index"
:content="item"
:index="index"
@click="handleDelete"
>
</todo-item>
</ul>
</div>
<script>
//全局组件 (vue中组件与实例的关系:vue的每一个组件都是vue的实例);
Vue.component('todo-item',{
props:['content','index'],//从副组件中接收content数据和index下标
template:'<li>{{content}} {{index}}</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 () {
alert(11);
}
}
});
</script>
</body>
</html>
template:'<li>{{content}} {{index}}</li>',在这里,你没有绑定事件

这个地方少了监听事件 正确应该为
<li @click="handleClick()">{{content}} {{index}}</li>
<todo-item>中应该是@delete而不是click
template中的li没有增加点击事件@click="handleClick"
增加以上两个,点击后会弹出对应的内容
如果需要删除:
handleDelete: function (index) {
this.list.splice(index,1)
}
自组件中,没有@click触发你的handleClick方法
除了@delete, handledelete方法写的也不对啊 handleDelete:function (index) {
this.list.spilice(index,1);
}
一样的,不过还是谢谢啦
父组件监听delete方法的那个你写的@click应该不对吧,不是应该是@delete吗?
vue2.5入门
147410 学习 · 675 问题
相似问题