vue獲取同級元件的數值或函式
阿新 • • 發佈:2021-03-07
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');