2

I have a global component on my VM, like this

Vue.component('dropdown', function () {
    template: require('./templates/Dropdown.template.html'),

    components: {
        clients: require('./../modal/Clients.js')
    }
});

As you can see this global component has its own child component - clients

In the template of this child component I have click event happening:

<button @click.prevent="submitForm"></button>

Question:

Is it possible that the first global component Dropdown would be responsible for handling this event?

So I would have it like this

Vue.component('dropdown', function () {

    ...

    methods: {
        submitForm: function () {
            // handling event
        }
    },

    ...
});

2 Answers 2

3

You can simply do this in your child component:

ChildComponent.vue

<button @click="$emit('click')">Child Component Button</button>

It will trigger the handler defined in your parent

ParentComponent.vue

<ChildComponent @click="myHandler()"/>
Sign up to request clarification or add additional context in comments.

Comments

-1

The preferred way to do this would be to send the event upwards using by $dispatch This avoids tight coupling which slightly defeats the purpose of independent, reusable components There is a very similar example in the documentation.

2 Comments

The VueJS developers removed the $dispatch method.
that's still a valid answer, there is no reason to downvote it. the API has changed, now one should use $emit() instead of dispatching the event

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.