最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass events easily from a child component to a parent in Vue 2 - Stack Overflow

programmeradmin2浏览0评论

I have a base ponent BaseInput.vue which accepts attributes and emits events. It's easy to bind all attributes by using v-bind="$attrs" instruction.

// 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 ponent WrapInput.vue which passes attributes to BasicInput and emits events.

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

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

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

My question: is there a handy way in Vue to pass a bunch of events in "proxy" ponents without the need to list them one by one? 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 {
  ponents: { BasicInput },
  inheritAttrs: false,
};
</script>

P.S. I've heard about EventBus, but doesn't fit nicely for my case.

I have a base ponent BaseInput.vue which accepts attributes and emits events. It's easy to bind all attributes by using v-bind="$attrs" instruction.

// 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 ponent WrapInput.vue which passes attributes to BasicInput and emits events.

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

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

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

My question: is there a handy way in Vue to pass a bunch of events in "proxy" ponents without the need to list them one by one? 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 {
  ponents: { BasicInput },
  inheritAttrs: false,
};
</script>

P.S. I've heard about EventBus, but doesn't fit nicely for my case.

Share Improve this question asked Feb 26, 2021 at 3:21 Eugene KarataevEugene Karataev 1,9711 gold badge15 silver badges24 bronze badges 1
  • I think you have a same question asked here: stackoverflow./questions/53825246/… – Yash Maheshwari Commented Feb 26, 2021 at 4:14
Add a ment  | 

2 Answers 2

Reset to default 11

In Vue 2, you could use v-on and $listeners to pass any event listeners to a ponent:

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

Vue 2 demo

In Vue 3, $listeners is removed, but any event listeners would be part of $attrs, so v-bind="$attrs" would suffice.

Vue 3 demo

Using Event Bus (Communication between any two ponents) An event bus is used to municate between any two ponents (Components need not have a parent-child relationship). This can be used when one needs to manually listen for events on a ponent instance.

You can just send the data from one ponent 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 ponent.

Source of the answer: https://dev.to/sanchithasr/how-to-municate-between-ponents-in-vue-js-kjc

发布评论

评论列表(0)

  1. 暂无评论