Author -  Sai gowtham

How to create custom events in VueJS

Custom events helps us to communicate to parent components in Vue.js.

Let’s learn how to create custom events in vue.js with the help of exmaples.

Suppose we have a Counter component which is accepting data from its parent component with the help of props.

Counter.vue
<template>
  <div>
    <h1>Counter {{ count }}</h1>
    <button>Increment</button>
  </div>
</template>

<script>
export default {
  props: ["count"]
};
</script>

From App component we are passing data to Counter component.

App.vue
<template>
  <div>
    <Counter :count="num"/>  </div>
</template>

<script>
import Counter from "./components/Counter";
export default {
  data: function() {
    return { num: 0 };
  },
  components: {
    Counter
  }
};
</script>

The problem is currently our Increment button present in Counter component can’t do anything.

when we click on a Increment button we need to increment the num data property present inside our App component, to do that we need to create a custom event inside our Counter component.

Custom event can be created by using this syntax this.$emit('eventname').

Counter.vue
<template>
  <div>
    <h1>Counter {{ count }}</h1>
    <button @click="increment">Increment</button>
  </div>
</template>

<script>
export default {
  props: ["count"],
  methods:{
      increment:function(){
          this.$emit('handleIncrement')      }
  }
};
</script>

Now our Counter component is emitting the handleIncrement custom event to listen this event in App component we need to add @handleIncrement.

App.vue
<template>
  <div>
    <!-- listening for the custom event -->    <Counter :count="num" @handleIncrement="num++"/>  </div>
</template>

<script>
import Counter from "./components/Counter";
export default {
  data: function() {
    return { num: 0 };
  },
  components: {
    Counter
  }
};
</script>

Passing arguments

Somecases we need to increment our count value by 2 or 3 in such cases this.$emit method can also accept the value as its second argument.

Counter.vue
<template>
  <div>
    <h1>Counter {{ count }}</h1>
    <button @click="incrementBy2">Increment</button>
  </div>
</template>

<script>
export default {
  props: ["count"],
  methods:{
      incrementBy2:function(){
          this.$emit('handleIncrement',2)      }
  }
};
</script>

In parent component we can access that argument value by using $event variable.

App.vue
<template>
  <div>
    <!-- listening for the custom event with emitted value -->    <Counter :count="num" @handleIncrement="num += $event;"/>  </div>
</template>

<script>
import Counter from "./components/Counter";
export default {
  data: function() {
    return { num: 0 };
  },
  components: {
    Counter
  }
};
</script>

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY