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 MySQL database transaction isolation levels

Database transaction isolation level There are 4 ...

Implementation code for using CSS text-emphasis to emphasize text

1. Introduction In the past, if you wanted to emp...

MYSQL transaction tutorial Yii2.0 merchant withdrawal function

Preface I am a PHP programmer who started out as ...

Vue implements a simple shopping cart example

This article example shares the specific code of ...

How to clear default styles and set common styles in CSS

CSS Clear Default Styles The usual clear default ...

MySQL paging performance exploration

Several common paging methods: 1. Escalator metho...

How to modify the MySQL character set

1. Check the character set of MySQL show variable...

Introduction to 10 online development tools for web design

1. Online Text Generator BlindTextGenerator: For ...

MySQL uses custom functions to recursively query parent ID or child ID

background: In MySQL, if there is a limited level...

Set the input to read-only via disabled and readonly

There are two ways to achieve read-only input: dis...

CSS uses radial-gradient to implement coupon styles

This article will introduce how to use radial-gra...

JavaScript to implement slider verification code

This article shares the specific code of JavaScri...

View the dependent libraries of so or executable programs under linux

View the dependent libraries of so or executable ...

How to export CSV file with header in mysql

Refer to the official document http://dev.mysql.c...