简体   繁体   中英

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..

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM