Skip to content

Instantly share code, notes, and snippets.

@unr
Created March 31, 2017 18:13
Show Gist options
  • Select an option

  • Save unr/ab4f84e651f0b0cb4322ca5f44f3d0ea to your computer and use it in GitHub Desktop.

Select an option

Save unr/ab4f84e651f0b0cb4322ca5f44f3d0ea to your computer and use it in GitHub Desktop.
Basic 'how to show modal component' vue 2.0 example
<template>
<div id='app'>
<component :is="currentModal" :modal-data="extraData" v-if="showModal"></component>
</div>
</template>
<script>
import LoginModal from './LoginModal';
import LogoutModal from './LogoutModal';
export default {
name: 'app',
components: {
LoginModal,
LogoutModal,
},
data() {
return {
currentModal: '',
extraData: null,
showModal: false,
};
},
created() {
// pass modalName to showModal
// additionally pass additional Modal data, if needed
this.$eventBus.$on('showModal', (modalName, modalData) => {
this.showModal = true;
this.currentModal = modalName;
this.extraData = modalData;
});
this.$eventBus.$on('closeModal', () => {
this.showModal = false;
});
},
};
</script>
// Setting up your main js
import Vue from 'vue';
// set up an eventBus listener, so you can call it anywhere.
// globally call Vue.$eventBus or
// call this.$eventBus from within a component.
const event = function event(Vue) {
Vue.prototype.$eventBus = Vue.$eventBus = new Vue();
};
Vue.use(event);
// set up your vue app
new Vue({
el: '#app',
template: '<App/>',
components: { App },
});
// This file should basically be a component that has a close method
// which basically calls the same
this.$eventBus.$emit('hideModal');
// event.
// Using THIS modal.vue components created // beforeDestroy methods, you can animate it in/out of your view.
<script>
export default {
name: 'someComponent',
created() {
// from any component, you can now trigger a modal
this.$eventBus.$emit('showModal', 'LoginModal');
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment