25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Modal window<br />

A lot of the functionality left in our root <strong>Vue</strong> instance concerns the modal<br />

window. Let's abstract this into a separate component. First, we'll create the new<br />

component file: $ touch resources/assets/components/ModalWindow.vue<br />

Now, we'll transplant the markup from the view to the component. To ensure the<br />

carousel stays decoupled from the modal window, we'll replace the ImageCarousel<br />

declaration in the markup with a slot.<br />

resources/assets/components/ModalWindow.vue: &times;<br />

<br />

<br />

We can now declare a ModalWindow element in the hole we just created in the view,<br />

with an ImageCarousel as content for the slot.<br />

resources/views/app.blade.php: ... ... <br />

We will now move the needed functionality from the root instance <strong>and</strong> place it<br />

inside the script tag.<br />

resources/assets/components/ModalWindow.vue: ... <br />

export default { data() { return { modalOpen: false } }, methods: {<br />

escapeKeyListener(evt) { if (evt.keyCode === 27 && this.modalOpen) {<br />

this.modalOpen = false; } } }, watch: { modalOpen() { var className = 'modalopen';<br />

if (this.modalOpen) { document.body.classList.add(className); } else {<br />

document.body.classList.remove(className); } } }, created() {<br />

document.addEventListener('keyup', this.escapeKeyListener); }, destroyed() {<br />

document.removeEventListener('keyup', this.escapeKeyListener); }, } <br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!