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:
|
A set of projects for training react+ts Although ...
The innodb_autoinc_lock_mode parameter controls t...
<br />Original text: http://jorux.com/archiv...
This article example shares the specific code for...
Empty link: That is, there is no link with a targ...
Table of contents 1. Supplementary knowledge poin...
environment Hostname ip address Serve Jenkins 192...
<br />Structure and hierarchy reduce complex...
The pagination component is a common component in...
Table of contents 1. First, let’s explain what Re...
Table of contents Routing Manager background gett...
Core code -- Below I will demonstrate the impleme...
According to the methods of the masters, the caus...
In the recent project, we need to create an effec...
These introduced HTML tags do not necessarily ful...