当前位置: 首页 > news >正文

vue3-父子通信

 

 

 

 一个简单的vue3子组件调用父组件方法的demo

<template>  <div>  <h2>Parent Component父组件</h2>  <ChildComponent @notify-parent="handleParentMethod" />  </div>  
</template>  <script>  
import { ref } from 'vue';  
import ChildComponent from './Child.vue';  export default {  name: 'Parent',  components: {  ChildComponent  },  setup() {  const handleParentMethod = () => {  console.log('Parent method called from child');  // 这里可以执行父组件的其他逻辑  };  return {  handleParentMethod  };  }  
};  
</script>
<template>  <div>  <h3>Child Component子组件</h3>  <button @click="notifyParent">Notify Parent</button>  </div>  
</template>  <script>  
import { defineComponent, emit } from 'vue';  export default defineComponent({  name: 'Child',  setup(_, { emit }) {  const notifyParent = () => {  emit('notify-parent'); // 触发自定义事件  };  return {  notifyParent  };  }  
});  
</script>

 在这个例子中,子组件(Child.vue)有一个按钮,当点击这个按钮时,它会触发一个名为 notify-parent 的自定义事件。父组件(Parent.vue)监听了这个事件,并在事件触发时调用 handleParentMethod 方法。

 

http://www.lryc.cn/news/376300.html

相关文章:

  • 微信小程序—页面滑动,获取可视区域数据
  • C#语言进阶(一)—委托
  • VST3音频插件技术介绍
  • MySQL数据库管理 二
  • android system UI 基础的基础
  • ARM32开发——GD32F4定时器查询
  • 【机器学习】第7章 集成学习(小重点,混之前章节出题但小题)
  • 代码随想录——子集Ⅱ(Leecode 90)
  • vue关闭页面时触发的函数(ai生成)
  • 马尔可夫性质与Q学习在强化学习中的结合
  • 【LeetCode 5.】 最长回文子串
  • 联邦学习周记|第四周
  • 机器学习课程复习——逻辑回归
  • Rocky Linux 更换CN镜像地址
  • Linux rm命令由于要删的文件太多报-bash: /usr/bin/rm:参数列表过长,无法删除的解决办法
  • 【包管理】Node.JS与Ptyhon安装
  • SpringMVC系列四: Rest-优雅的url请求风格
  • Hexo 搭建个人博客(ubuntu20.04)
  • 【论文阅读】-- Attribute-Aware RBFs:使用 RT Core 范围查询交互式可视化时间序列颗粒体积
  • A类IP介绍
  • HTML5基本语法
  • 正则表达式常用表示
  • 【OpenHarmony4.1 之 U-Boot 2024.07源码深度解析】007 - evb-rk3568_defconfig 配置编译全过程
  • 11.1 Go 标准库的组成
  • 【UG\NX二次开发】UF 调用Grip例子(实现Grip调用目标dll)(UF_call_grip)
  • [算法刷题积累] 两数之和以及进阶引用
  • pytest+parametrize+yaml实例
  • 【HarmonyOS】鸿蒙应用模块化实现
  • 深入Node.js:实现网易云音乐数据自动化抓取
  • 【Docker实战】jenkins卡在编译Dockerfile的问题