Home > Web Front-end > Vue.js > body text

What is the usage of $emit in vue

WBOY
Release: 2022-03-17 11:31:41
Original
6268 people have browsed it

In vue, "$emit" is used to trigger events on the current instance, and nearby parameters will be passed to the listener callback; subcomponents can use "$emit" to trigger custom events of the parent component, and the syntax is: "vm.$emit( ​​event, [...args] )".

What is the usage of $emit in vue

The operating environment of this article: Windows 10 system, Vue version 2.9.6, DELL G3 computer.

What is the usage of $emit in vue

Use $emit(eventName) in vue to trigger events

$emit(eventName) triggers events on the current instance, with additional parameters will be passed to the listener callback.

Use $emit(eventName) to trigger events

Explanation in Api:

vm.$emit( event, […args] )
Copy after login

Usage of $emit in vue

1. The parent component can Use props to pass data to child components.

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

vm.$emit( event, arg ) //触发当前实例上的事件
vm.$on( event, fn );//监听event事件后运行 fn;
Copy after login

Examples are as follows:

Child component

<template>  
  <div class="train-city">  
    <h3>父组件传给子组件的toCity:{{sendData}}</h3>   
    <br/><button @click=&#39;select(`大连`)&#39;>点击此处将‘大连’发射给父组件</button>  
  </div>  
</template>  
<script>  
  export default {  
    name:&#39;trainCity&#39;,  
    props:[&#39;sendData&#39;], // 用来接收父组件传给子组件的数据  
    methods:{  
      select(val) {  
        let data = {  
          cityname: val  
        };  
        this.$emit(&#39;showCityName&#39;,data);//select事件触发后,自动触发showCityName事件  
      }  
    }  
  }  
</script>
Copy after login

Parent component:

<template>  
    <div>父组件的toCity{{toCity}}</div>  
    <train-city @showCityName="updateCity" :sendData="toCity"></train-city>  
<template>  
<script>  
  import TrainCity from "./train-city";  
  export default {  
    name:&#39;index&#39;,  
    components: {TrainCity},  
    data () {  
      return {  
        toCity:"北京"  
      }  
    },  
    methods:{  
      updateCity(data){//触发子组件城市选择-选择城市的事件  
        this.toCity = data.cityname;//改变了父组件的值  
        console.log(&#39;toCity:&#39;+this.toCity)  
      }  
    }  
  }  
</script>
Copy after login

[Related recommendations: "vue.js tutorial 》】

The above is the detailed content of What is the usage of $emit in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!