<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="main">
<!--这里要么写成parent,要么写成parent($event),不能写成parent(),这样写会收不到子组件传过来的值-->
<child @child-emit="parent"></child>
<div>{{childPassParent}}</div>
</div>
<script>
Vue.component('child',{
template:`
<div>
{{childText}}
<button @click="childClick()">child-click</button>
</div>
`,
data() {
return {
childText: '子组件文本'
}
},
methods: {
childClick() {
let value = '子组件传递过来的数值:' + Math.random();
this.$emit('child-emit',value);
}
}
});
new Vue({
el: '#main',
data: {
childPassParent: ''
},
methods: {
parent(event) {
console.log(event);
this.childPassParent = event;
}
}
})
</script>
</body>
</html>
组件传值之子传父
最新推荐文章于 2024-08-18 13:53:12 发布