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

父子组件中,子组件调用父组件的方法

父子组件中,子组件调用父组件的方法

方法一:直接在子组件中通过this.$parent.event来调用父组件的方法

父组件

<template><p><child>父组件</child></p>
</template>
<script>import child from '~/components/dam/child';export default {components: {child},methods: {fatherMethod() {}}};
</script>

子组件

<template><p><button @click="childMethod()">子组件</button></p>
</template>
<script>export default {methods: {childMethod() {this.$parent.fatherMethod();}}};
</script>

方法二:在子组件里用$emit向父组件触发一个事件,父组件监听这个事件即可

父组件

<template><p><child @fatherMethod="fatherMethod">父组件</child></p>
</template>
<script>import child from '~/components/dam/child';export default {components: {child},methods: {fatherMethod() {}}};
</script>

子组件

<template><p><button @click="childMethod()">子组件</button></p>
</template>
<script>export default {methods: {childMethod() {this.$parent.fatherMethod();}}};
</script>

方法三:父组件把方法传入子组件中,在子组件里直接调用这个方法

父组件

<template><p><child @fatherMethod="fatherMethod">父组件</child></p>
</template>
<script>import child from '~/components/dam/child';export default {components: {child},methods: {fatherMethod() {}}};
</script>

子组件

<template><p><button @click="childMethod()">子组件</button></p>
</template>
<script>export default {props: {fatherMethod: {type: Function,default: null}},methods: {childMethod() {if (this.fatherMethod) {this.fatherMethod();}}}};
</script>
http://www.lryc.cn/news/16634.html

相关文章:

  • 第七章.深度学习
  • 小学生学Arduino---------点阵(三)动态的显示与清除
  • opencv图片处理
  • C++ Primer Plus 学习笔记(二)—— 复合类型
  • 代码随想录算法训练营第七天 | 454.四数相加II 、 383. 赎金信、15. 三数之和、18. 四数之和 、总结
  • apply函数族
  • 读书笔记可读性素材
  • 【C++】vector 模拟实现
  • canvas初体验
  • JavaWeb12-线程通讯(线程等待和唤醒)
  • 江苏专转本如何事半功倍的备考
  • linux下安装mongoDB
  • 掌握MySQL分库分表(七)广播表、绑定表实战,水平分库+分表实现及之后的查询和删除操作
  • 企业为什么需要数据可视化报表
  • 5个有效的华为(HUAWEI)手机数据恢复方法
  • 【Java并发编程】线程安全(一)Synchronized原理
  • [apollo]vue3.x中apollo的使用
  • system()函数启用新进程占有原进程的文件描述符表的问题
  • nignx(安装,正反代理,安装tomcat设置反向代理,ip透传)
  • sklearn模块常用内容解析笔记
  • 我的 System Verilog 学习记录(2)
  • 【调研报告】Monorepo 和 Multirepo 的风格对比及使用示例
  • Retrofit源码分析
  • Mybatis-Plus入门系列(20) -兼容多种数据库
  • JetPack板块—Android X解析
  • C++学习笔记-数字
  • Nginx——Nginx的基础原理
  • 服务端开发Java之备战秋招面试篇1
  • 【C++的OpenCV】第三课-OpenCV图像加载和显示
  • 【面试1v1实景模拟】Spring事务 一文到底