0

In my child component I have an custom click handler that emits an event like this:

this.$emit('nextslide');

And in my parent component I try to catch the event like this:

render(createElement) {
    return createElement(
        'div',
        {
            on: {
                nextslide: this.triggerFunction,
            }
        },

        this.$slots.default,
    )
},

I get no errors at all but it is also not working. I'm wondering if you can actually catch and event like this.

The page:

<carousel>
    <slide-next>
    </slide-next>
</carousel>

Carousel.vue:

<script>
    export default {

        render(createElement) {
            return createElement(
                'div',
                {
                    on: {
                        nextslide: this.triggerFunction,
                    }
                },
                this.$slots.default,
            )
        },

        methods: {
            triggerFunction() {
                console.log('this should trigger');
            },
        }
    }
</script>

SlideNext.vue

<script>
    export default {

        render(createElement) {
            return createElement(
                'div',
                {
                    on: {
                        click: this.clickHandler
                    },

                },
                'Next')
        },

        methods: {

          clickHandler() {
              this.$emit('nextslide');
              console.log('trigger emit');
          },

        },

    }
</script>

2 Answers 2

1

So you want in parent component, to listen to event created in child component.Take a look here: See it in action

Child component:

Vue.component('child-component', {
    data: function() {
    return {
        text: 'hi i am a text from child component'
    }
  },
  render(h) {
    return h('div', {
        class: ['text'],
      on: {
        click: this.clicked
      }
    },
    ['Click me,please']
    )
  },
  methods: {
    clicked() {
        this.$emit('click', this.text)
    }
  }
})

Parent component:

Vue.component('parent-component', {
    render (h) {
    return h('child-component', {
        on: {
        click: this.clicked
      }
    })
  },
  methods: {
    clicked(data_passed_from_child) {
        alert(`From child passed: ${data_passed_from_child}`)
    }
  }
})

And finally to make this to work:

<div id="app">
  <parent-component></parent-component>
</div>

new Vue({
  el: "#app",
})
Sign up to request clarification or add additional context in comments.

4 Comments

This works! but how do I return more child components?
I answer your question and you have to appreciete this i think.Anyway you can take a look here for another questions: vuejs.org/v2/guide/render-function.html
I really do appreciate it.
Glad to help you
1

You're registering the listener on a <div> element instead of the component. Component events do not bubble, unlike native DOM events, so you can only register the nextslide event on the child component vnode directly instead of an ancestor.

I can't see from your example where the relationship between the parent and child component is – your parent component isn't rendering the child component directly (unless it's in the default slot).

3 Comments

Ah makes sense... Yes my child component is in the default slot. I am not clear on the "so you can only register the nextslide event on the child component vnode directly" do I have to move my triggerFunction to my child component instead?
All I meant by that was you can only have the { on: { nextslide: this.triggerFunction } } bit for the child component (whatever that is) and not a 'div'.
I edited my question with the components, would you mind to take a look and help me with how I can achieve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.