自定义事件实现子传父

父亲定义事件,子接收后触发事件

父:

复制代码
<template>
  <div class="father">
    父:<br/>
    <!-- 父给子绑定一个事件,事件名叫xxoo,触发的函数是fuk,需要在父方定义函数fuk -->
    <Child @xxoo="fuk"/>
  </div>
</template>

<script setup lang="ts" name="Father">
 import Child from "./Child.vue";
 import { ref } from 'vue'
  
  function fuk(val){
    alert(val)
  }

</script>

<style scoped>
    .father{
        background-color:rgb(165, 164, 164);
        padding: 20px;
        border-radius: 10px;
    }
</style>
复制代码

子:

复制代码
<template>
  <div class="child">
   子:<br/>
   <button @click="emit('xxoo','daye')" >btn</button>
  </div>
</template>

<script setup lang="ts" name="Child">
  import { ref } from 'vue'
// 子接收父定义的事件名xxoo
const emit= defineEmits(['xxoo'])
// 子触发xxoo事件,后面接参数,也可以在按钮触发
emit('xxoo','nima')
</script>

<style scoped>
    .child{
        background-color: skyblue;
        padding: 10px;
        box-shadow: 0 0 10px black;
        border-radius: 10px;
    }
</style>
复制代码

 

posted @   关键步就几步  阅读(6)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示