Adding Click Event To Component - gecko-8/devwiki GitHub Wiki

Use Case

Say you want to create a custom button component and pass the click event to a contained button control.

Making it Happen

  1. Add a click event to your contained button and add a handler method to it.
    <template>
      <button @click="onClick">
        <slot></slot>
      </button>
    </template>
    
    <script>
    export default {
      methods: {
        onClick() {
          // Next step will be added here
        }
      }
    }
    </script>
    
  2. Emit a click event at the spot indicated above.
    <script>
    export default {
      methods: {
        onClick() {
          this.$emit('click');
        }
      }
    }
    </script>
    
  3. Handle it as usual wherever you use your component.
    @click="onClick"
    
⚠️ **GitHub.com Fallback** ⚠️