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

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.

主站蜘蛛池模板: 怀柔区| 永仁县| 安远县| 大同县| 阿巴嘎旗| 隆林| 锡林浩特市| 札达县| 阿拉善左旗| 北辰区| 庐江县| 梧州市| 那坡县| 临泽县| 申扎县| 永靖县| 永平县| 金溪县| 汉寿县| 沛县| 阿瓦提县| 兴宁市| 安乡县| 门头沟区| 华容县| 辉南县| 班玛县| 枣阳市| 安远县| 资源县| 运城市| 丘北县| 兴安县| 巴楚县| 突泉县| 郓城县| 安仁县| 渝北区| 台中县| 饶河县| 宜丰县|