vue获取同级组件的数值或函数

8 篇文章 0 订阅
vm.$emit( event, arg ) //触发当前实例上的事件

vm.$on( event, fn );//监听event事件后运行 fn; 
1.获取同级组件的数值(B获取A的值)

组件A:dataManage.vue
组件B:echartsPie.vue
在这里插入图片描述

(1)新建一个newVue.js
import Vue from 'vue'
export default new Vue({})
(2)在两个组件中都引入一个事件总线(名字随意取)
import ePie from './newVue'
(3)B中添加$emit函数

传一个参

ePie.$emit('listenShow',isShow);

传多个参

ePie.$emit('pieData',[cy, yl, shfw, cxzs, hw, xy]);
(4)A中添加$on函数

传一个参

ePie.$on('listenShow', (...val) => {
          this.isShow = val[0][0];
        });

传多个参

ePie.$on('pieData', (...val) => {
          this.cy = val[0][0];
          this.yl = val[0][1];
          this.shfw = val[0][2];
          this.cxzs = val[0][3];
          this.hw = val[0][4];
          this.xy = val[0][5];
        });
2.获取同级组件的函数(B获取A的函数)

组件A:dataManage.vue
组件B:echartsPie.vue
在这里插入图片描述

(1)在A中编写需要传递的函数

在这里插入图片描述

(2)A中添加$on函数
var that = this;
ePie.$on('echartsMethod', function() {
          that.myEcharts()
        });
(3)B中添加$emit函数
ePie.$emit('echartsMethod');
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值