简体   繁体   English

在 Vue 2 中轻松地将事件从子组件传递给父组件

[英]Pass events easily from a child component to a parent in Vue 2

I have a base component BaseInput.vue which accepts attributes and emits events.我有一个基本组件BaseInput.vue ,它接受属性并发出事件。 It's easy to bind all attributes by using v-bind="$attrs" instruction.使用v-bind="$attrs"指令很容易绑定所有属性。

// BaseInput.vue
<template>
  <label>
    <input
      v-bind="$attrs"
      @focus="$emit('focus')"
      @blur="$emit('blur')"
    >
  </label>
</template>

<script>
export default {
  inheritAttrs: false,
};
</script>

Next I have a wrapper component WrapInput.vue which passes attributes to BasicInput and emits events.接下来我有一个包装器组件WrapInput.vue ,它将属性传递给BasicInput并发出事件。

// WrapInput.vue
<template>
  <basic-input
    v-bind="$attrs"
    @focus="$emit('focus')"
    @blur="$emit('blur')"
  />
</template>

<script>
import BasicInput from '@/storybook/pages/BasicInput';

export default {
  components: { BasicInput },
  inheritAttrs: false,
};
</script>

My question: is there a handy way in Vue to pass a bunch of events in "proxy" components without the need to list them one by one?我的问题:在 Vue 中是否有一种方便的方法可以在“代理”组件中传递一堆事件而无需一一列出? I expect something like this:我期待这样的事情:

// WrapInput.vue
<template>
  <basic-input
    v-bind="$attrs"
    v-bind="$events" // is it possible to proxy all events in a single line?
  />
</template>

<script>
import BasicInput from '@/storybook/pages/BasicInput';

export default {
  components: { BasicInput },
  inheritAttrs: false,
};
</script>

PS I've heard about EventBus, but doesn't fit nicely for my case. PS 我听说过 EventBus,但不太适合我的情况。

In Vue 2, you could use v-on and $listeners to pass any event listeners to a component:在 Vue 2 中,您可以使用v-on$listeners将任何事件侦听器传递给组件:

// BasicInput.vue
<input v-on="$listeners">

Vue 2 demo Vue 2 演示

In Vue 3, $listeners is removed , but any event listeners would be part of $attrs , so v-bind="$attrs" would suffice.在 Vue 3 中,移除$listeners ,但任何事件监听器都将成为$attrs的一部分,因此v-bind="$attrs"就足够了。

Vue 3 demo Vue 3 演示

Using Event Bus (Communication between any two components) An event bus is used to communicate between any two components (Components need not have a parent-child relationship).使用事件总线(任意两个组件之间的通信) 事件总线用于在任意两个组件之间进行通信(组件不需要有父子关系)。 This can be used when one needs to manually listen for events on a component instance.当需要手动侦听组件实例上的事件时,可以使用此功能。

You can just send the data from one component using this.$root.$emit('name-of-emitter', args1, args2, ...) and is captured using the same name like this this.$root.$on('name-of-emitter', args1, args2, ...) in the other component.您可以使用this.$root.$emit('name-of-emitter', args1, args2, ...)从一个组件发送数据,并使用类似 this this.$root.$on('name-of-emitter', args1, args2, ...)的相同名称捕获this.$root.$on('name-of-emitter', args1, args2, ...)在另一个组件中。

Source of the answer: https://dev.to/sanchithasr/how-to-communicate-between-components-in-vue-js-kjc答案来源: https://dev.to/sanchithasr/how-to-communicate-between-components-in-vue-js-kjc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM