<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<Fa />
</div>
<!-- 父组件 -->
<template id="fa">
<div class="fa">
父组件
<son @fafun="buy" />
</div>
</template>
<!-- 子组件 -->
<template id="son">
<div class="son">
子组件
<button @click="sonFun">sonBtn</button>
</div>
</template>
<script>
Vue.component('Fa', {
template: "#fa",
data () {
return {
}
},
methods: {
buy(num1, num2) {
console.log('faFun', num1, num2)
}
}
})
Vue.component('Son', {
template: "#son",
methods: {
sonFun() {
console.log('sonFun')
// 参数1:自定义事件名 其他参数
this.$emit('fafun', 666, 888) // 子组件触发父组件的方法
}
}
})
new Vue({
el: '#app',
data: {
}
})
</script>
</body>
</html>```
vue子组件控制父组件数据 emit
最新推荐文章于 2023-08-04 07:45:00 发布