Detailed explanation of the use of $emit in Vue.js

Detailed explanation of the use of $emit in Vue.js

1. Parent components can use props to pass data to child components.

2. Child components can use $emit to trigger custom events of parent components.

vm.$emit( ​​event, arg ) //Trigger the event on the current instance

vm.$on( event, fn ); //Run fn after listening to event;

For example: subcomponent:

<template>
  <div class="train-city">
    <h3>ToCity passed from parent component to child component:{{sendData}}</h3> 
    <br/><button @click='select(`Dalian`)'>Click here to emit 'Dalian' to the parent component</button>
  </div>
</template>
<script>
  export default {
    name:'trainCity',
    props:['sendData'], // Used to receive data passed from the parent component to the child component methods:{
      select(val) {
        let data = {
          cityname: val
        };
        this.$emit('showCityName',data);//After the select event is triggered, the showCityName event is automatically triggered}
    }
  }
</script>

Parent component:

<template>
    <div>
        <div>Parent component's toCity{{toCity}}</div>
        <train-city @showCityName="updateCity" :sendData="toCity"></train-city>
    </div>
<template>
<script>
  import TrainCity from "./train-city";
  export default {
    name:'index',
    components: {TrainCity},
    data () {
      return {
        toCity:"Beijing"
      }
    },
    methods:{
      updateCity(data){//Trigger the child component city selection-select city event this.toCity = data.cityname;//Change the value of the parent component console.log('toCity:'+this.toCity)
      }
    }
  }
</script>

Figure 1: Click on the previous data

Figure 2: Data after clicking

This is the end of this article about the detailed case study of $emit usage in Vue.js. For more relevant content on the usage of $emit in Vue.js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue.js+boostrap project practice (case detailed explanation)
  • Vue.js implements tab switching and color change operation explanation
  • Detailed explanation of the usage of scoped slots in Vue.js slots
  • Vue.js implements calendar function
  • Vue.js implements timeline function
  • Vue.js manages the encapsulation of background table components
  • Ideas and practice of multi-language solution for Vue.js front-end project
  • 10 Best Practices for Building and Maintaining Large-Scale Vue.js Projects

<<:  Solve the problem of qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in Qt under Ubuntu 18.04

>>:  How to solve the problem of not finding the password after decompressing the MySQL free installation version

Recommend

MySQL character types are case sensitive

By default, MySQL character types are not case-se...

Use of Linux tr command

1. Introduction tr is used to convert or delete a...

Detailed explanation of MySQL table name case-insensitive configuration method

By default, MySQL in Linux distinguishes between ...

CSS scroll-snap scroll event stop and element position detection implementation

1. Scroll Snap is a must-have skill for front-end...

Detailed explanation of JavaScript prototype and examples

Table of contents The relationship between the co...

Django+vue registration and login sample code

register The front-end uses axios in vue to pass ...

How to expand the disk partition for centos system

Problem/failure/scenario/requirement The hard dis...

Summary of the use of MySQL date and time functions

This article is based on MySQL 8.0 This article i...

Nginx tp3.2.3 404 problem solution

Recently I changed Apache to nginx. When I moved ...

MySQL paging analysis principle and efficiency improvement

MySQL paging analysis principle and efficiency im...

Tutorial on building file sharing service Samba under CentOS6.5

Samba Services: This content is for reference of ...

MySQL aggregate function sorting

Table of contents MySQL result sorting - Aggregat...

Vue+node realizes audio recording and playback function

Result: The main part is to implement the code lo...

Start a local Kubernetes environment using kind and Docker

introduce Have you ever spent a whole day trying ...

Docker builds jenkins+maven code building and deployment platform

Table of contents Docker Basic Concepts Docker in...