Detailed explanation of custom events of Vue components

Detailed explanation of custom events of Vue components

insert image description here

<template>
  <div >
    <h2>{{msg}}</h2>
    <!-- Implemented by passing function-type data props from parent component to child component: child passes data to parent -->
<School :getName="getName"/>
<Student :getStudentname="getStudentname"/>
<!-- Bind a custom event to the child component through the parent component: realize the child to parent data transfer-->
<Age v-on:elderSex="demo"/>
<!-- Bind a custom event to the child component through the parent component: the child passes data to the parent (the second way of writing: using ref) -->
   <!-- <Student ref="student"/> -->
  </div>
</template>
<script>
import School from './components/School.vue'
import Student from './components/Student.vue'
import Age from './components/Age.vue'
export default {
name:'App',
components:{School,Student,Age},
data(){
  return {
    msg:'Hello, world! '
  }
},
methods:{ 
  getName(name){
    console.log('App received the name',name);
  },
  getStudentname(name1){
    console.log('Received the student's name',name1);
  },
  demo(sex1){
    console.log('demo is called',sex1);
  }
},
// mounted() {
  //Bind custom events // this.$refs.student.$on('elderSex',this.schoolAge)
//Bind custom event (one-time)
// this.$refs.student.$once('elderSex',this.schoolAge)
// },
}
</script>
<style scoped>
</style>
<template>
  <div class="demo">
    <h2>Student name: {{name}}</h2>
    <h2>Student age: {{age}}</h2>
    <button @click="sendStudentname">Send the student's name to the APP</button>
  </div>
</template>
<script>
  export default {
    name: 'Student',
    props: ['getStudentname'],
    data() {
      return {
        name: 'Zhang San',
        age: 19
      }
    },
    methods: {
      sendStudentname() {
        this.getStudentname(this.name)
      }
    }
  }
</script>
<style>
  .demo {
    background-color: skyblue;
  }
</style>

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue learning notes: Example of binding native events to components
  • vue component add event @click.native action
  • Functional diagram of custom events in Vue components
  • How to bind click events to custom components in Vue

<<:  CSS overflow-wrap new property value anywhere usage

>>:  Small details of web front-end development

Recommend

Detailed explanation of Linux file permissions and group modification commands

In Linux, everything is a file (directories are a...

JavaScript to implement image preloading and lazy loading

This article shares the specific code for impleme...

JavaScript to achieve JD.com flash sale effect

This article shares the specific code of JavaScri...

HTML basic syntax is convenient for those who are just starting to learn HTML

1.1 General marking A general tag consists of an ...

React implements a highly adaptive virtual list

Table of contents Before transformation: After tr...

Talking about the practical application of html mailto (email)

As we all know, mailto is a very practical HTML ta...

When modifying a record in MySQL, the update operation field = field + string

In some scenarios, we need to modify our varchar ...

Sample code for seamless scrolling with flex layout

This article mainly introduces the sample code of...

JS array loop method and efficiency analysis comparison

Array Methods JavaScript has provided many array ...

Detailed explanation of nodejs built-in modules

Table of contents Overview 1. Path module 2. Unti...

Vue parent component calls child component function implementation

Vue parent component calls the function of the ch...

A brief discussion on the calculation method of key_len in mysql explain

The MySQL explain command can analyze the perform...

Detailed explanation of formatting numbers in MySQL

Recently, due to work needs, I need to format num...