Vue.js Events - $emit

If you're just getting started with this series of Vue.js posts, you should start with the Vue.js Starter Page.

Vue.js uses events to pass data from a child component up to a parent component, specifically calling the event $emit. Here's how:

Vue.js Events (via $emit)

This file is actually a combination of 2 different Vue files; App.vue (the parent component) and Header.vue (the child component).

In a recent discussion comparing Primitive vs. Reference Types, string data altered in the Vue.js framework (primitive) is changed directly in the component and affects only the variable in that specific component where the change was made. Changes made to data sets, like arrays (reference types) affect all instances in all components. So in order to make primitive data behave and update just like reference types, Vue.js uses events ($emit) to broadcast changes UP from a child component to a parent component that is prepared to listen for such changes.

Line 6 shows the child component's v-on:click that will trigger the method changeTitle - also in the child component - on line 24. This function changeTitle() uses $emit to broadcast the event also named changeTitle up to the parent component (App.vue) where a listener has been setup via a v-on:changeTitle on line 44. When received, the updateTitle method on line 78 accepts the passed argument (here it's the event object $event). We arbitrarily refer to the passed argument in this method as updatedTitle causing the method to fire and complete.

<!--Header.vue template-->
<!-- thanks to -->

		<h1 v-on:click="changeTitle">{{title}}</h1>

	export default {
				title:'Vue Ninjas'
				this.$emit(changeTitle,'My latest title');

<!-- add "scoped" attribute to style tag to ensure that only elements in this component get style applied, else it goes global -->
<style scoped>


<!-- end Header.vue-->

<!-- start App.vue-->

    <app-header v-bind:title="title" v-on:changeTitle="updateTitle($event)"></app-header>
	<app-footer v-bind:title="title"></app-footer>


	import Header from './components/Header.vue';
	import Footer from './components/Footer.vue';
	import Muscles from './components/Muscle.vue';

export default {
  name: 'app',
  data () {
    return {
			{name: 'Pectoralis', area:'Chest', show:false},
			{name: 'Biceps', area:'Arm', show:false},
			{name: 'Triceps', area:'Arm', show:false},
			{name: 'Latissimus', area:'Back', show:false},
			{name: 'Quadriceps', area:'Leg', show:false},
			{name: 'Soleus', area:'Calf', show:false}
   		title:"Muscle Title Here"

	#app {
	  font-family: 'Avenir', Helvetica, Arial, sans-serif;
	  -webkit-font-smoothing: antialiased;
	  -moz-osx-font-smoothing: grayscale;
	  text-align: center;
	  color: #2c3e50;
	  margin-top: 60px;

	h1, h2 {
	  font-weight: normal;

	ul {
	  list-style-type: none;
	  padding: 0;

	li {
	  display: inline-block;
	  margin: 0 10px;

	a {
	  color: #42b983;

	<!-- end App.vue-->

Link your website to this page! Copy and paste the URL below: