官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 墨竹工卡县| 贵港市| 阿克苏市| 曲麻莱县| 南丰县| 伊春市| 嘉鱼县| 榆树市| 平阳县| 冕宁县| 青冈县| 陵水| 乐清市| 宜春市| 博爱县| 宜章县| 红安县| 洛扎县| 台州市| 汉源县| 寻甸| 周至县| 曲靖市| 盐津县| 柳江县| 柘荣县| 册亨县| 三门县| 射洪县| 泗水县| 和平区| 特克斯县| 桂阳县| 神池县| 芦溪县| 监利县| 五寨县| 高要市| 双流县| 上思县| 措美县|