• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • v-on

    • 缩写:@
    • 预期:Function | Inline Statement | Object
    • 参数:event
    • 修饰符:

      • .stop- 调用event.stopPropagation()
      • .prevent- 调用event.preventDefault()
      • .capture- 添加事件侦听器时使用 capture 模式。
      • .self- 只当事件是从侦听器绑定的元素本身触发时才触发回调。
      • .{keyAlias}- 仅当事件是从特定键触发时才触发回调。
      • .once- 只触发一次回调。
      • .left- 只当点击鼠标左键时触发。
      • .right- 只当点击鼠标右键时触发。
      • .middle- 只当点击鼠标中键时触发。
      • .passive- {passive: true}模式添加侦听器

    用法:绑定事件监听器。事件类型由参数指定。表达式可以是一个方法的名字或一个内联语句,如果没有修饰符也可以省略。

    用在普通元素上时,只能监听原生 DOM 事件。用在自定义元素组件上时,也可以监听子组件触发的自定义事件。监听原生 DOM 事件时,方法以事件为唯一的参数。如果使用内联语句,语句可以访问一个$event property:v-on:click="handle('ok',$event)"

    v-on同样支持不带参数绑定一个事件/监听器键值对的对象。注意当使用对象语法时,是不支持任何修饰器的。


    <!-- 方法处理器 -->
    <button v-on:click="doThis"></button>
    
    <!-- 动态事件 -->
    <button v-on:[event]="doThis"></button>
    
    <!-- 内联语句 -->
    <button v-on:click="doThat('hello', $event)"></button>
    
    <!-- 缩写 -->
    <button @click="doThis"></button>
    
    <!-- 动态事件缩写 -->
    <button @[event]="doThis"></button>
    
    <!-- 停止冒泡 -->
    <button @click.stop="doThis"></button>
    
    <!-- 阻止默认行为 -->
    <button @click.prevent="doThis"></button>
    
    <!-- 阻止默认行为,没有表达式 -->
    <form @submit.prevent></form>
    
    <!-- 串联修饰符 -->
    <button @click.stop.prevent="doThis"></button>
    
    <!-- 键修饰符,键别名 -->
    <input @keyup.enter="onEnter" />
    
    <!-- 点击回调只会触发一次 -->
    <button v-on:click.once="doThis"></button>
    
    <!-- 对象语法 -->
    <button v-on="{ mousedown: doThis, mouseup: doThat }"></button>
    


    在子组件上监听自定义事件(当子组件触发“my-event”时将调用事件处理器):

    <my-component @my-event="handleThis"></my-component>
    
    <!-- 内联语句 -->
    <my-component @my-event="handleThis(123, $event)"></my-component>
    

    上篇:v-show

    下篇:v-bind