I'm using single file components triggered from vue-router. I have a components/Header.vue which uses a child modals/HelpModal.vue which in turn has a child components/Modal.vue.
I'm trying to pass around events in order to keep Modal.vue re-usable, but I can't seem to access this.show from within an event in HelpModal.vue
Modal.vue
<template>
<div class="modal">
<button class="modal__close" @click="$emit('close')">
</button>
<slot></slot>
</div>
</template>
HelpModal.vue
<template>
<modal v-if="show" @close="closeModal">...help modal content...</modal>
</template>
<script>
import Modal from 'components/Modal.vue'
import bus from 'eventBus'
export default {
components: {
Modal
},
data: () => {
return {
show: false
}
},
methods: {
closeModal: () => {
this.show = false
}
},
mounted: () => {
bus.$on('showHelpModal', () => {
console.log('show modal!', this.show); // undefined
this.show = true
})
}
}
Header.vue
<template>
<header>
<button @click="showHelpModal">Show help</button>
<help-modal></help-modal>
</header>
</template>
<script>
import HelpModal from 'views/modals/HelpModal.vue'
import bus from 'eventBus'
export default {
components: {
HelpModal
},
methods: {
showHelpModal: () => {
bus.$emit('showHelpModal')
}
}
}
</script>
I may be going about this all totally wrong, but it's the only way I can see it working to have a link in the header that triggers the help modal to open