埋坑一: vue中子组件调用兄弟组件方法
小计: 开发中遇到子组件需要调用兄弟组件中的方法,如下写个小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中子组件调用兄弟组件方法的更多相关文章
- Vue中子组件调用父组件的方法
Vue中子组件调用父组件的方法 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- VUE中的子父组件、兄弟组件之间相互传值,相互调用彼此的方法
vue--组件传值 父组件传值给子组件--"props" 一.父组件--示例 <template> <child :choose-data="choos ...
- vue中子组件调用父组件里面的数据和方法 父组件调用子组件的数据和方法
1.子组件直接调用父组件的数据和方法 在父组件father,vue <template> <div> <!-- 父组件里面的数据 --> <p>父组件里 ...
- Vue子组件调用父组件的方法
Vue子组件调用父组件的方法 Vue中子组件调用父组件的方法,这里有三种方法提供参考 第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法 父组件 <temp ...
- vue 子组件调用父组件的方法
vue中 父子组件的通信: 子组件通过 props: { //子组件中写的. childMsg: { //字段名 type: Array,//类型 default: [0,0,0] //这样可以指定默 ...
- Vue 子组件调用父组件 $emit
<!DOCTYPE html><html> <head> <meta charset="utf-8"> ...
- Vue 父组件调用子组件的方法
qwq 前两天看了下vue,父子组件方法的调用,怕忘记,所以做个小记录. 一.父组件调用子组件的方法 1.父组件 <template> <div id="rightmen ...
- vue 子组件调用父组件的函数
子组件调用父组件的函数,使用$emit(eventName,[...args]),触发当前实例上的事件.附加参数都会传给监听器回调. 子组件 <template> <div> ...
- vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等
vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...
随机推荐
- wsl和windows相互访问文件夹
How to access Windows folders from Bash on Ubuntu on Windows You'll find the Windows C:\ structure a ...
- jmeter添加自定义扩展函数之小写转换大写
1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...
- iOS开发环境搭建 及 编写1个hello world
参照: https://www.cnblogs.com/ansersion/p/9084460.html 前置条件 : MAC一台 安装xcode,从appstore 下载 xcode,(6G多,考验 ...
- Cocos2d Box2D之简介
| 版权声明:本文为博主原创文章,未经博主允许不得转载. Box2D是一个用于模拟2D刚体物体的C++引擎.Box2D集成了大量的物理力学和运动学的计算,并将物理模拟过程封装到类对象中,将对物体的 ...
- Cocos2d-x中使用的数据容器类
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 在计算机的数据结构中,有着数组,链表,堆栈,队列,树,图,哈希表等一些结构.在面向对象的语言中,这些结构被封装成了特定的类,而这些类就是容 ...
- 39.Binary Tree Inorder Traversal(二叉树中序遍历)
Level: Medium 题目描述: Given a binary tree, return the inorder traversal of its nodes' values. 思路分析: ...
- 五、hibernate表与表之间的关系(一对多关系)
数据库表与表之间的关系 一对多:一个学校可以有多个学生,一个学生只能有一个学校 多对多:一个学生可以有多个老师,一个老师可以教多个学生 一对一:一个人只能有一个身份证号,一个身份证号只能找到一个人 一 ...
- 2018-2-13-关于Host(主机)
title author date CreateTime categories 关于Host(主机) lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 ...
- 2019 ICPC Universidad Nacional de Colombia Programming Contest C D J
C. Common Subsequence 题意:给出长度为n两个串,求两个串的最长公共子序列len,如果len>=0.99*n,两个串就是亲兄弟否则不是. 解法:朴素的求LCS的时间复杂度是O ...
- Codeforces 362E 费用流
题意及思路:https://blog.csdn.net/mengxiang000000/article/details/52472696 代码: #define Hello the_cruel_wor ...