Vuejs - 冒泡的自定义活动

问题描述 投票:5回答:2

有没有办法在组件中使用组件时允许事件冒泡?

我的应用程序是一个动态菜单。动态菜单是一个组件(dyn-menu),它为每个menu-item元素使用局部组件(<li>)。每个<menu-item>都有一个与之关联的点击处理程序,它发出一个自定义事件(在完整实现中具有菜单项的ID)。但该应用程序没有看到<menu-item>发布的事件,因为它们没有冒泡。

有没有办法允许<menu-item>组件本地的<dyn-menu>组件发出事件并仍允许vapp查看和处理事件?

我对Vuejs很新,所以我可能会遗漏一些明显的东西。而且我可能试图通过使用两个组件来解决这个问题,而这并不是处理它的最佳方法。是否有更好的方法来接近它?

这是一个jsfiddle。你必须删除@dyn-menu-item-click='itemClick'模板中的<dyn-menu>行,以说明如果组件不处理事件,事件不会冒泡。如果该线被移除,那么<dyn-menu>不处理该事件,但vapp也从未看到该事件。

javascript event-handling vue.js
2个回答
3
投票

我知道有4种选择

  1. 像你一样重新发射事件
  2. 在子组件上使用this.$parent(重复)来访问所需的父级并发出事件。 (参见下面的“实现自己的冒泡事件插件”)
  3. 使用父母的provided和孩子们的injected事件总线。
  4. 使用Vuex存储并将事件推送到子组件中的事件队列。在应用程序的其他位置,观察新元素的反应事件队列或仅将其绑定到某个东西。

Implement your own bubbling event plugin

这很简单。该插件添加了一个新的$bubble方法,可以发出冒充他们父母的事件。我考虑发布一个执行此操作的插件,但它很简单,开销不值得。

// Add this as a Vue plugin
Vue.use((Vue) => {
  Vue.prototype.$bubble = function $bubble(eventName, ...args) {
    // Emit the event on all parent components
    let component = this;
    do {
      component.$emit(eventName, ...args);
      component = component.$parent;
    } while (component);
  };
});

// Some nested components as an example

// note usage of "$bubble" instead of "$emit"
Vue.component('component-c', {
  template: `
    <button type="button" @click="$bubble('my-event', 'payload')">
      Emit bubbling event
    </button>`,
});

Vue.component('component-b', {
  template: `<component-c @my-event="onMyEvent" />`,
  
  methods: {
    onMyEvent(...args) {
      console.log('component-b listener: ', ...args);
    },
  },
});

Vue.component('component-a', {
  template: `<component-b @my-event="onMyEvent" />`,
  
  methods: {
    onMyEvent(...args) {
      console.log('component-a listener: ', ...args);
    },
  },
});

var vapp = new Vue({
  el: '#app',

  methods: {
    onMyEvent(...args) {
      console.log('root listener: ', ...args);
    },
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <component-a @my-event="onMyEvent" />
</div>

Event bus

事件总线看起来像这样:

Vue.component('dyn-menu', {
  components: {
    'menu-item': {
      template: '<li @click="itemClick">{{item.text}}</li>',
      props: ['item'],
      inject: ['eventBus'], // <-- Inject in the child
      methods: {
        itemClick() {
          // Emit the event on the event bus
          this.eventBus.$emit('dyn-menu-item-click', ['menu-item dyn-menu-item-click']);
        }
      }
    }
  },

  // ...
});

var vapp = new Vue({
  el: '#app',
  data: {
    // ...
    eventBus: new Vue(),
  },
  provide() {
    return {
      // The parent component provides the event bus to its children
      eventBus: this.eventBus,
    };
  },

  created() {
    // Listen to events on the event bus
    this.eventBus.$on('dyn-menu-item-click', this.menuClick);
  },
  methods: {
    menuClick(message) {}
  }
})

工作示例:https://jsfiddle.net/7vwfx52b/

这里列出了很多事件总线插件:https://github.com/vuejs/awesome-vue#custom-events


1
投票

从Vue 2.4开始,组件可以通过$listeners属性访问其父级的侦听器。通过将属性v-on="$listeners"添加到这些子元素的标记,可以将组件设置为通过其父级侦听器传递给特定子级。请参阅https://vuejs.org/v2/api/#vm-listeners上的文档。

您还可以使用以下属性转发特定事件:@dyn-menu-item-click=$listeners['dyn-menu-item-click']

它仍然不是真正的冒泡,而是一种不那么冗长的重新发射事件的方式。

© www.soinside.com 2019 - 2024. All rights reserved.