VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 基础

  • 组件

    • Component Usage Details
    • Passing Data from Parent to Child Components
    • Child Component Emitting Events and Values to Parent
    • Prop Validation and Non-Prop Attributes
    • Custom Events
      • Sibling Component Communication
      • Non-Parent-Child Component Communication
      • Parent Calling Child Component Methods with Arguments
      • Slots
      • Dynamic Components and v-once Directive
      • Vue Parent-Child Component Lifecycle Order
    • 过渡&动画

    • 可复用性&组合

    • 工具

    • 规模化

    • Vuex

    • 其他

    • 《Vue》笔记
    • 组件
    xugaoyi
    2020-02-15
    Contents

    Custom Events

    # Custom Events

    API (opens new window)

    # Events bound with v-on on component tags are custom events

    <div id="root">
        <child @click="handleClick"></child> <!--here click is a custom event-->
    </div>
    
    <script type="text/javascript">
        Vue.component('child', {
            template: '<button>Child</button>',
        })
    
        var vm = new Vue({
            el: '#root'
            methods: {
                handleClick() {
                    alert('click')
                }
            }
        })
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    In the code above, clicking the child component will NOT trigger the handleClick() event, because click here is not a native click event but a custom event.

    To trigger the click event on the component tag, you need to emit it from the child component using $emit:

    <div id="root">
        <child @click="handleClick"></child>
    </div>
    
    <script type="text/javascript">
        Vue.component('child', {
            template: '<button @click="handleChildClick">Child</button>', // click here is a native event
            methods: {
                handleChildClick() {
                    this.$emit('click')
                }
            }
        })
    
        var vm = new Vue({
            el: '#root'
            methods: {
                handleClick() {
                    alert('click')
                }
            }
        })
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    In the code above, the click inside the child component is a native click event, which emits a click custom event via $emit to trigger the parent's handleClick().

    # Turning a custom event into a native event

    Use the .native modifier to turn a custom event into a native event:

    <div id="root">
        <child @click.native="handleClick"></child> <!--custom event with native modifier becomes a native event-->
    </div>
    
    <script type="text/javascript">
        Vue.component('child', {
            template: '<button>Child</button>',
        })
    
        var vm = new Vue({
            el: '#root'
            methods: {
                handleClick() {
                    alert('click')
                }
            }
        })
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    In the code above, the custom event click has the .native modifier added, making it a native event and thus triggering handleClick().

    # Custom event names are automatically lowercased

    When using camelCase for custom event names:

    this.$emit('myEvent')
    
    1

    Listening for the kebab-case version of that name will have no effect:

    <!-- no effect -->
    <my-component v-on:my-event="doSomething"></my-component>
    
    1
    2

    v-on event listeners inside DOM templates will be automatically converted to all lowercase (because HTML is case-insensitive), so v-on:myEvent will become v-on:myevent -- making it impossible to listen for myEvent.

    Therefore, we recommend always using kebab-case for event names.

    Edit (opens new window)
    #Vue
    Last Updated: 2026/03/21, 12:14:36
    Prop Validation and Non-Prop Attributes
    Sibling Component Communication

    ← Prop Validation and Non-Prop Attributes Sibling Component Communication→

    Recent Updates
    01
    How I Discovered Disposable Email — A True Story
    06-12
    02
    Animations in Grid Layout
    09-15
    03
    Renaming a Git Branch
    08-11
    More Articles >
    Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
    • Auto
    • Light Mode
    • Dark Mode
    • Reading Mode