学习笔记《Vue 事件处理》

看到同事写的代码,「@submit.native.prevent」一下子抓不到含义,发现 Vue 在 2.0 以后添加了一些「事件(Event)」方面的功能,学习一下,以作备忘

真相:
.native 表示对一个组件绑定系统原生事件
.prevent 表示提交以后不刷新页面

注册

<button v-on:click="greet">Greet</button> // greet 是 greet() 的简写
<button v-on:click="say('hi')">Say hi</button>
<button v-on:click="warn($event)"> // original DOM event
<button @click="greet"> // for short

事件修正符

<!-- the click event's propagation will be stopped(组织传播) -->
<a v-on:click.stop="doThis"></a>
<!-- the submit event will no longer reload the page(不刷新页面) -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- modifiers can be chained(构建链路) -->
<a v-on:click.stop.prevent="doThat"></a>
<!-- just the modifier(省略事件名) -->
<form v-on:submit.prevent></form>
<!-- use capture mode when adding the event listener(捕获给内部使用) -->
<!-- i.e. an event targeting an inner element is handled here before being handled by that element -->
<div v-on:click.capture="doThis">...</div>
<!-- only trigger handler if event.target is the element itself(只对自己有效) -->
<!-- i.e. not from a child element -->
<div v-on:click.self="doThat">...</div>
<!-- the click event will be triggered at most once(单次有效) -->
<a v-on:click.once="doThis"></a>

按键修正符

按键修正符支持方便的扩展和自定义,默认的有:

.enter(键盘)
.tab
.delete (captures both “Delete” and “Backspace” keys)
.esc
.space
.up
.down
.left
.right
.ctrl(快捷键)
.alt
.shift
.meta
.left(鼠标)
.right
.middle
.native - listen for a native event on the root element of component.
.passive - (2.3.0+) attaches a DOM event with { passive: true }

除了以上几个以外,所有定义在 JavaScript 中的 KeyboardEvent.key 都可以被直接使用(转换为 kebab-case 格式)

<!-- same as above -->
<input v-on:keyup.enter="submit">
<!-- case from KeyboardEvent.key -->
<input @keyup.page-down="onPageDown">

<!-- Alt + C -->
<input @keyup.alt.67="clear">
<!-- Ctrl + Click -->
<div @click.ctrl="doSomething">Do something</div>

<!-- this will fire even if Alt or Shift is also pressed(也许不是你需要的效果) -->
<button @click.ctrl="onClick">A</button>
<!-- this will only fire when only Ctrl is pressed(.exact 可以帮你解决) -->
<button @click.ctrl.exact="onCtrlClick">A</button>

(在组件上)自定义事件

Listen to an event using $on(eventName)
Trigger an event using $emit(eventName) ($emit 也可以从子组件中触发父组件中注册的事件)

<el-dialog @close="close">
</el-dialog>

vm.$on('test', function (msg) {
  console.log(msg)
})
vm.$emit('test', 'hi')

绑定系统原生事件的时候,需要加 .native 修正符

<my-component v-on:click.native="doTheThing"></my-component>

疑问

.passive 这个修正符的意思我没有 get 到:

A seemingly minor, but incredibly welcome change in the latest Vue release is the addition of the .passive event modifier to v-on. This allows an event to be bound in such a way that it explicitly says that it won’t cancel the event. This translates to a significant performance improvement on mobile for certain events, such as wheel, touchstart, and touchmove.

We’ll be covering the passive event modifier in JavaScript in the next few days.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,430评论 0 23
  • SYY是我的同事,我认识她已经二十多年了。二十年来,我们从相识到相知,到成为无话不谈的好姐妹,是因为我们的三观一致...
    心安若水筱筠阅读 3,408评论 0 0
  • ★ 芒格智慧箴言与私人书单— 如果你想获得你要的东西,那就让自己配得上它。信任、成功和钦佩都是靠努力获得的。— 成...
    雪野兔阅读 3,051评论 0 0
  • 羡慕之心人人皆有,当羡慕过度就会变成嫉妒。 有时,发现身边的人因为某些方面优秀而备受关注,自己却平淡无奇的时候,表...
    芦苇辫阅读 4,084评论 0 1
  • 树叶微黄的夏之尽头 提着沉重行李箱的女孩穿过不熟悉的街 熙攘安宁的矛盾之地 遇到 陌生的人 听到 陌生的声音 看到...
    YJia阅读 2,350评论 0 2