Skip to content

Instantly share code, notes, and snippets.

@lloydjatkinson
Created January 22, 2021 14:15
Show Gist options
  • Save lloydjatkinson/dfc488122659436caffa68f5d60570ff to your computer and use it in GitHub Desktop.
Save lloydjatkinson/dfc488122659436caffa68f5d60570ff to your computer and use it in GitHub Desktop.
Vue Dialog/Modal/Toast component with v-model binding for closing and opening

Dialog.vue:

<template>
    <div>
        <button @click="close">Close</button>
        <slot />
    </div>
</template>

<script>
export default {
    name: 'Dialog',
    
    props: {
        value: {
            type: Boolean,
            required: true,
            default: false,
        },
    },
    
    close () {
        this.$emit('input', false);
    },
}
</script>

Another component:

<template>
    <div>
        <button @click="openDialog">Show Dialog</button>
        <Dialog v-model="showDialog">
            Hello, world!
        </Dialog>
    </div>
</template>

<script>
export default {
    name: 'Dialog',
    
    data () {
        return {
            showDialog: false,
        }
    },
    
    methods: {
        openDialog () {
            this.showDialog = true;
        }
    }
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment