25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Event modifiers<br />

<strong>Vue</strong> makes it easy to listen for specific keys by offering modifiers to the v-on<br />

directive. Modifiers are postfixes denoted by a dot (.), for example: <br />

As you'd probably guess, the .enter modifier tells <strong>Vue</strong> to only call the h<strong>and</strong>ler<br />

when the event is triggered by the Enter key. Modifiers save you from having to<br />

remember the specific key code, <strong>and</strong> also make your template logic more<br />

obvious. <strong>Vue</strong> offers a variety of other key modifiers, including:<br />

tab<br />

delete<br />

space<br />

esc<br />

With that in mind, it seems like we could close our modal with this directive: v-<br />

on:keyup.esc="modalOpen = false"<br />

But then what tag do we attach this directive to? Unfortunately, unless an input is<br />

focused on, key events are dispatched from the body element, which, as we know,<br />

is out of <strong>Vue</strong>'s jurisdiction!<br />

To h<strong>and</strong>le this event we'll, once again, resort to the Web API.<br />

app.<strong>js</strong>: var app = new <strong>Vue</strong>({ ... }); document.addEventListener('keyup',<br />

function(evt) { if (evt.keyCode === 27 && app.modalOpen) { app.modalOpen<br />

= false; } });<br />

This works, with one caveat (discussed in the next section). But <strong>Vue</strong> can help us<br />

make it perfect.

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

Saved successfully!

Ooh no, something went wrong!