<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>组件元素删除</title>
<!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> -->
<!-- <script src="https://unpkg.com/vue"></script> -->
<script src="js/vue.min.js"></script>
</head>
<body>
<div id="root">
<div>
<input v-model="inputValue">
<button @click="handleSubmit">提交</button>
</div>
<ul>
<!-- handleDelete是父组件的方法,写在父组件的模板中 -->
<todo-item v-for="(item,index) of list" :key="index" :content="item"
:index="index" @delete="handleDelete"></todo-item>
</ul>
</div>
<script type="text/javascript">
// 全局组件
Vue.component('todo-item', {
// template: '<li>item</li>'
props:['content'],
template:'<li @click="handleClick">{{content}}</li>',
methods:{
handleClick:function() {
// $emit触发事件,名称为delete
this.$emit("delete", this.index);
}
}
})
// 局部组件
/*var TodoItem = {
template:'<li>item</li>'
}*/
new Vue({
el: "#root",
/*components:{
'todo-item':TodoItem
},*/
data:{
inputValue:"",
list:[]
},
methods:{
handleSubmit:function() {
this.list.push(this.inputValue);
this.inputValue = "";
},
handleDelete:function(index) {
this.list.splice(index,1);
}
}
})
</script>
</body>
</html>
vue练习7-2
最新推荐文章于 2024-07-03 23:36:40 发布