Detailed explanation of value transfer between parent and child components in Vue3

Detailed explanation of value transfer between parent and child components in Vue3

It has been a long time since the birth of vue3, and the author has only recently started learning vue3. Compared with vue2, vue3 has undergone significant changes in writing. The most typical example is that vue3 implements data responsiveness through ref or reactive. Because of the emergence of ref and reactive, the way of passing values ​​between parent and child components in Vue3 has also changed.

Let's first look at how to write it in vue2

Parent component:

<!-- Parent component -->
<template>
  <div>
    <children :title="title" @getChildren="getChildren"></children>
    <div>Child component says: {{ childrenAsk }}</div>
  </div>
</template>
 
<script>
  import children from "./children.vue"
  export default {
    data() {
      return {
        title: "I am the value passed from the parent component",
        childrenAsk: ""
      }
    },
    methods: {
      getChildren(val) {
        this.childrenAsk = val
      }
    }
  }
</script>

Subcomponents:

<!-- Child Component -->
<template>
  <div>
    <div>Value passed from the parent component: {{ title }}</div>
    <button @click="askToFather">Click to send to the parent component</button>
  </div>
</template>
<script>
  export default {
    props: {
      title:
        type: String
      }
    },
    data() {
      return {
        askMsg: "This is what I said to the parent component"
      }
    },
    methods: {
      askToFather() {
        this.$emit("getChildren", this.askMsg)
      }
    }
  }
</script>

In vue2, the value transfer from the child component to the parent component is implemented through this.$emit, but in vue3, this does not exist. In vue3, both data and functions are encapsulated in setup, so how does vue3 implement it?

We know that setup in vue3 receives two parameters. The first parameter is props, which is the props value passed from the parent component to the child component. The second value is context, which represents the current context object. Now that we know this, we can implement the parent-child component value transfer of vue3.

The parent-child inheritance in Vue3 is the same as the parent-child inheritance in Vue2. I will not elaborate on it again. The focus below is on the child-parent inheritance in Vue3.

Parent Component

<template>
  <div style="color: aqua">Parent component</div>
  <div>The child component says to me: {{ children_msg }}</div>
  <children :title="msg" @listen="listenToChildren"></children>
  {{ value }}
</template>
<script lang="ts">
import children from "@/views/component_emit/children.vue"
import { defineComponent, ref } from "vue"
export default defineComponent({
  components:
    children,
  },
  name: "father",
  setup() {
    let msg = "I am the parent component"
    let children_msg = ref("") // The function of ref is to implement responsiveness. If there is no ref, responsiveness cannot be implemented (reference data types use reactive)
    let listenToChildren = (val) => {
      children_msg.value = val // To use data wrapped by ref, you need to access its value in the form of .value}
    return {
      msg,
      children_msg,
      listenToChildren,
    }
  },
})
</script>
<style></style>

Subcomponents:

<template>
  <div style="color: brown">Subcomponents</div>
  <!-- The method of parent-child transmission is the same as vue2-->
  <div>The value passed from the parent component is: {{ title }}</div>
  <button @click="sayToFather">Speak to the parent component</button>
</template>
<script lang="ts">
import { defineComponent } from "vue"
export default defineComponent({
  name: "children",
  props: {
    title:
      type: String,
    },
  },
  setup(props, context) {
    // The purpose of context is to obtain the context object.
    // If setup is written as setup(props, { emit }), the following context can be omitted const sayToFather = () => {
      const ask = "I am a child component, I talk to the parent component"
      context.emit("listen", ask)
    }
    return {
      sayToFather,
    }
  },
})
</script>
<style></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:
  • Data transfer, modification and update methods of Vue parent-child components
  • How to let child components modify parent component data in Vue
  • Detailed explanation of the problem of parent-child value transfer two-way binding and data update in Vue
  • Vue3.0 father-son parameter transfer, child modification of parent data

<<:  Discussion on style customization and browser compatibility issues when using input element [type="file"]

>>:  How to Use rsync in Linux

Recommend

Introduction to the use of MySQL official performance testing tool mysqlslap

Table of contents Introduction Instructions Actua...

Recommend a cool flashing alarm button

The effect is as follows: The code is as follows ...

CSS example code to hide the scroll bar and scroll the content

Preface When the HTML structure of a page contain...

React Principles Explained

Table of contents 1. setState() Description 1.1 U...

Nginx source code compilation and installation process record

The installation of the rpm package is relatively...

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

Vue.$set failure pitfall discovery and solution

I accidentally found that Vue.$set was invalid in...

Record a pitfall of MySQL update statement update

background Recently, I executed a DML statement d...

How to use shtml include

By applying it, some public areas of the website c...

Getting Started Tutorial for Beginners ⑨: How to Build a Portal Website

Moreover, an article website built with a blog pro...

25 Examples of Using Circular Elements in Web Design

Today, this post lists some great examples of circ...

A question about border-radius value setting

Problem Record Today I was going to complete a sm...

How to install PostgreSQL11 on CentOS7

Install PostgreSQL 11 on CentOS 7 PostgreSQL: The...

Essential bonus items for optimizing and packaging the front end of Vue projects

Table of contents Preface 1. Routing lazy loading...

Detailed description of common events and methods of html text

Event Description onactivate: Fired when the objec...