1.父组件向子组件传值
2、子组件向父组件传值的过程:通过this.$emit('自定义事件',传出的值)来实现
3、完整代码,可以自行引入官网的vue.js 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="./vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.7/dist/vue.js"></script>
<title>todolist功能开发</title>
</head>
<body>
<div id="root">
<div>
<input v-model="inputValue" type="text">
<button @click="handleSubmit">提交</button>
</div>
<ul>
<!-- <li v-for="(item,index) of list" :key="index">{{item}}</li> -->
<todo-item v-for="(item,index) of list"
@delete="handleDelete"
:key="index"
:content="item"
:index="index"
>
</todo-item>
</ul>
</div>
<script>
Vue.component('todo-item',{//全局组件 自定义组件
props:['content','index'],//接收属性的值
template:'<li @click=handleClick>{{content}}</li>',
methods:{
handleClick:function(){
console.log(this.index);
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>