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

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.

主站蜘蛛池模板: 陵川县| 麻栗坡县| 兖州市| 科技| 蓬安县| 民勤县| 肥乡县| 固原市| 安西县| 武功县| 北海市| 泾阳县| 扎鲁特旗| 吉木乃县| 尉犁县| 九台市| 开原市| 潼南县| 青河县| 舟山市| 陈巴尔虎旗| 汉川市| 文化| 昆山市| 富源县| 英超| 延津县| 宽城| 金门县| 武城县| 大竹县| 哈巴河县| 崇仁县| 临泉县| 天镇县| 湖州市| 章丘市| 衡山县| 会东县| 北辰区| 唐山市|