0

I am pretty new to Vue.js.

I have a requirement wherein I want to bind events to the generic button template.

I tried using the below method.

    Vue.component('ac-btn', {
    props: [
        'clickevent'
    ],
    methods: {
        greet: function () {
            alert("I am Called");
        }
    },  
    template: '<button type="button" :click="clickevent" class="btn btn-secondary btn-sm"><slot>button</slot></button>'
});

But the click even is not called. Any idea of how to bind the click event to generic button template dynamically?

Edit:

@Saurabh

I tried your suggestion above and below is how it looks in my project.

Vue.component('ac-parent', {
    methods: {
        greet: function () {
            alert("Hiey");
        }
    },
    template: `<div class="container-fluid">
    <div class="row">
        <div class="col-lg-3 col-md-4">
            <div>
                <ac-child :onClick="greet">Custom value to replace generic slot value</ac-child>
            </div>
        </div>
     </div>`
});

Vue.component('ac-child', {
 template: '<button type="button" @click="clickevent" class="btn btn-secondary btn-sm"><slot>button</slot></button>'
});

Still this doesn't work. Am I doing something wrong?

2
  • Shouldn't​ it be :click="greet" ? Commented Jan 27, 2018 at 5:26
  • Thanks @SaurabhGour for your quick reply. Yes, it should be :click="greet", but like I mentioned I want it to bind dynamically. So, anything that I pass into props.clickevent is the function that should bind to the button. Commented Jan 27, 2018 at 5:29

2 Answers 2

1

It's not good idea to pass callback through component prop :onClick="greet"... Better way is to emit event from child to parent component this.$emit('click', event), so you can catch this event in parent component and do something... After this you can use this button component like native button with <ac-child @click="greet">Custom Button</ac-child>

Sign up to request clarification or add additional context in comments.

Comments

0

Please try using below code:

app.js

Vue.component('ac-parent', {
    methods: {
    greet: function () {
        alert("Hiey");
    }
},
template: `<div class="container-fluid">
    <div class="row">
        <div class="col-lg-3 col-md-4">
            <div>
                <ac-child :onClick="greet">Custom value to replace generic slot value</ac-child>
            </div>
        </div>
    </div>
    </div>`
});

Vue.component('ac-child', {
    props: ['onClick'],   
    template: '<button type="button" @click="onClick" class="btn btn-secondary btn-sm"><slot>button</slot></button>'
});

new Vue({
    el: '#vue-app'
})

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Title</title>        
        <script src="https://unpkg.com/vue"></script>
    </head>
    <body>
        <div id="vue-app">
            <ac-parent></ac-parent>               
        </div>
    </body>
    <script src="./app.js"></script>
</html>

Do let me know in case you still face any issues

1 Comment

Thanks for your help. The idea suggested by @Nikola Spalevic worked for me.

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.