- Full-Stack Vue.js 2 and Laravel 5
- Anthony Gore
- 175字
- 2021-07-02 19:57:23
Event modifiers
Vue makes it easy to listen forspecifickeys by offeringmodifiersto thev-ondirective. Modifiers are postfixes denoted by a dot (.), for example:
<input v-on:keyup.enter="handler">
As you'd probably guess, the.entermodifiertells Vue to only call the handler when the event is triggered by theEnterkey. Modifiers save you from having to remember the specific key code, and also make your template logic more obvious. Vue offers a variety of other key modifiers, including:
- tab
- delete
- space
- esc
With that in mind, it seems like we could close our modal with this directive:
v-on:keyup.esc="modalOpen = false"
But then what tag do we attach this directive to? Unfortunately, unless an input is focused on, key events are dispatched from thebodyelement, which, as we know, is out of Vue's jurisdiction!
To handle this event we'll, once again, resort to the Web API.
app.js:
var app = new Vue({ ... }); document.addEventListener(</span>'keyup', function(evt) { if (evt.keyCode === 27 && app.modalOpen) { app.modalOpen = false; } });
This works, with one caveat (discussed in the next section). But Vue can help us make it perfect.
- Android應用程序開發與典型案例
- Vue.js 3.x從入門到精通(視頻教學版)
- Mastering Scientific Computing with R
- Java程序設計:原理與范例
- 前端HTML+CSS修煉之道(視頻同步+直播)
- Julia高性能科學計算(第2版)
- Getting Started with Eclipse Juno
- ServiceNow:Building Powerful Workflows
- 詳解MATLAB圖形繪制技術
- Python程序設計教程
- Java程序設計實用教程(第2版)
- 算法訓練營:海量圖解+競賽刷題(入門篇)
- Unity 3D UI Essentials
- Python Django Web從入門到項目實戰(視頻版)
- Mastering PostgreSQL 11(Second Edition)