EventBus 子组件、弹窗等触发父组件事件,或组件相互传值

创建event-bus.js文件,内容如下:

import Vue from 'vue';
export const EventBus = new Vue();

在父子组件内分别引入:

import { EventBus } from './js/event-bus';

父组件定义触发后的操作,或传的值:

created() {
    EventBus.$on('refreshTreeKey', () => {
        this.treeKey++;
    });
},
destroyed() {
    EventBus.$off('refreshTreeKey');
},

created() {
  EventBus.$on('currentRow', (data) => {
    this.currentRow = data;
  });
},
destroyed() {
  EventBus.$off('currentRow');
},

子组件触发父组件自定义事件:

EventBus.$emit('refreshTreeKey');

EventBus.$emit('currentRow', this.currentRow);

相关推荐

  1. vue3 组件组件

    2024-05-16 09:18:13       35 阅读
  2. uniapp组件组件

    2024-05-16 09:18:13       42 阅读
  3. React组件组件

    2024-05-16 09:18:13       36 阅读
  4. Vue实现组件组件

    2024-05-16 09:18:13       16 阅读
  5. VUE中组件组件进行

    2024-05-16 09:18:13       10 阅读
  6. vue3:组件如何给组件

    2024-05-16 09:18:13       5 阅读
  7. vue组件调用组件的方法 传递组件

    2024-05-16 09:18:13       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-16 09:18:13       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-16 09:18:13       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-16 09:18:13       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-16 09:18:13       18 阅读

热门阅读

  1. 代码随想录算法训练营day26 | 77. 组合

    2024-05-16 09:18:13       9 阅读
  2. AI学习指南数学工具篇-在python中使用PCA

    2024-05-16 09:18:13       15 阅读
  3. android关于adb相关命令梳理

    2024-05-16 09:18:13       14 阅读
  4. Mysql慢查询优化

    2024-05-16 09:18:13       11 阅读
  5. Oracle数据块之数据块事务槽中的SCN

    2024-05-16 09:18:13       11 阅读
  6. Internal Validity vs Construct Validity

    2024-05-16 09:18:13       9 阅读