Giesburts
Giesburts

Reputation: 7668

vue.js $emit custom event not working

I am new to vue.js (2). I am writing vanilla JS.

When I try to use a custom event (close) I get a syntax error of ", expected" and ": expected". What I want is to add a custom close event to a component in the view. Then, in the template of the component I try to have the click event reach the custom close event. It is not working..

HTML

<div id="root" class="container">
    <bulma-modal v-if="showBulmaModal" @close="showBulmaModal = false"></bulma-modal>
    <button @click="showBulmaModal = true" class="button">Show modal</button>
</div>

JS

Vue.component('bulma-modal', {
    template: '<div class="modal is-active"><div class="modal-background"></div><div class="modal-content"><div class="box"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></div></div><button class="modal-close" @click="$emit('close')"></button></div>'
});

new Vue({
    el: '#root',
    data: {
        showBulmaModal: false
    }
});

Is there something I can't see or I am doing wrong? I can't get it right..

Upvotes: 1

Views: 2068

Answers (1)

Bert
Bert

Reputation: 82489

You need to escape the single quotes you use inside your template.

template: '<div class="modal is-active"><div class="modal-background"></div><div class="modal-content"><div class="box"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></div></div><button class="modal-close" @click="$emit(\'close\')">Close</button></div>'

Here is your code working.

Upvotes: 3

Related Questions