小计: 开发中遇到子组件需要调用兄弟组件中的方法,如下写个小demo记录下心得,如果你有好的方法,请到评论区域指教

父组件示例代码:

组件功能解析:
通过$emit获取子组件事件,通过$ref调用子组件中事件,实现子组件二的click事件
调用兄弟组件一中的事件
<template>
<div>
<!-- 子组件1 -->
<son1 ref="borther" :dataFromFather="dataFromFather"></son1>
<!-- 子组件2 -->
<son2 @triggerBrotherMethods="triggerBrotherMethods" :dataFromFather="dataFromFather"></son2>
</div>
</template> <script>
// 引入子组件一
import son1 from './son1'
// 引入子组件二
import son2 from './son2' export default {
data() {
return {
dataFromFather: []
}
},
// 注册子组件
components: {
son1,
son2
},
methods: {
// 子组件2中click事件
triggerBrotherMethods() {
// 父组件通过$ref调用子组件1中的事件方法
this.$refs.borther[0].bortherMethods()
},
}
}
</script> <style lang="less" scoped>
/* .... */
</style>

子组件一

组件功能解析:
加载父组件数据,进行业务操作
<template>
<!-- 子组件son2 -->
<div @click="bortherMethods">
<!-- 父组件传值展示 -->
{{dataFromFather}}
</div>
</template> <script>
export default {
data() {
return {
}
},
props: ['dataFromFather'],
methods: {
// 兄弟组件中的按钮事件
bortherMethods() {
// 子组件事件方法
...
},
}
}
</script> <style lang="less" scoped>
/* .... */
</style>

子组件二:

组件功能解析:
加载父组件数据,通过click事件emit传给父组件
<template>
<!-- 子组件son2 -->
<div @click="triggerBrotherMethods">
<!-- 父组件传值展示 -->
{{dataFromFather}}
</div>
</template> <script>
export default {
data() {
return {
}
},
props: ['dataFromFather'],
methods: {
// 触发兄弟组件中的按钮事件
triggerBrotherMethods() {
this.$emit('clickBrotherBtn', true)
},
}
}
</script> <style lang="less" scoped>
/* .... */
</style>

埋坑一: vue中子组件调用兄弟组件方法的更多相关文章

  1. Vue中子组件调用父组件的方法

    Vue中子组件调用父组件的方法 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  2. VUE中的子父组件、兄弟组件之间相互传值,相互调用彼此的方法

    vue--组件传值 父组件传值给子组件--"props" 一.父组件--示例 <template> <child :choose-data="choos ...

  3. vue中子组件调用父组件里面的数据和方法 父组件调用子组件的数据和方法

    1.子组件直接调用父组件的数据和方法 在父组件father,vue <template> <div> <!-- 父组件里面的数据 --> <p>父组件里 ...

  4. Vue子组件调用父组件的方法

    Vue子组件调用父组件的方法   Vue中子组件调用父组件的方法,这里有三种方法提供参考 第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法 父组件 <temp ...

  5. vue 子组件调用父组件的方法

    vue中 父子组件的通信: 子组件通过 props: { //子组件中写的. childMsg: { //字段名 type: Array,//类型 default: [0,0,0] //这样可以指定默 ...

  6. Vue 子组件调用父组件 $emit

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...

  7. Vue 父组件调用子组件的方法

    qwq  前两天看了下vue,父子组件方法的调用,怕忘记,所以做个小记录. 一.父组件调用子组件的方法 1.父组件 <template> <div id="rightmen ...

  8. vue 子组件调用父组件的函数

    子组件调用父组件的函数,使用$emit(eventName,[...args]),触发当前实例上的事件.附加参数都会传给监听器回调. 子组件 <template> <div> ...

  9. vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等

    vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...

随机推荐

  1. python使用开源图片识别第三方库tesseract

    详细安装博客:https://blog.csdn.net/luanyongli/article/details/81385284 第一步tesseract-ocr的安装如果不会请参照:https:// ...

  2. kvm动态修改内存和cpu

    https://www.cnblogs.com/nmap/p/6369180.html

  3. css3D动画

    css3D动画 前言 最近玩了玩用css来构建3D效果,写了几个demo,所以博客总结一下. 在阅读这篇博客之前,请先自行了解一下css 3D的属性,例如:transform-style,transf ...

  4. Spring Boot学习一之配置类及自动配置

    一.配置类 1. 导入其他配置类 你不需要将所有的 @Configuration 放进一个单独的类, @Import 注解可以用来导入其他配置类.另外,你也可以使用 @ComponentScan 注解 ...

  5. 解决Ubuntu与Windows双系统时间不同步问题

    目录 1.Windows修改法 1.1设置UTC 1.2恢复LocalTime 2.Ubuntu修改法 2.1设置LocalTime 2.2恢复UTC 切换系统后,往往发现时间差了8小时.这恰恰是北京 ...

  6. 用MR生成HFile文件格式后,数据批量导入HBase

    环境hadoop cdh5.4.7 hbase1.0.0 测试数据: topsid  uid roler_num typ 10 111111 255 0 在Hbase 创建t2数据库: create ...

  7. P1001 A+B Problem

    下面我们要展示这个题的玄学做法!!!@cjx!!! #include<iostream> #define chen using namespace std; #define jia int ...

  8. maven私服的项目使用配置

    环境:  eclipse .maven.nexus. 1.配置setting.xml文件 1.1.配置本地仓库位置:文件中,存在节点 “localRepository”,默认是注释,也就是本地仓库使用 ...

  9. top查看进程的参数

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按键来不 ...

  10. spring集成rabbitMq(非springboot)

    首先 , pom文件需要加入spring集成rabbitMq的依赖: <dependency> <groupId>org.springframework.amqp</gr ...