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

Implementation of vue-nuxt login authentication

Table of contents introduce Link start Continue t...

Cross-browser local storage Ⅰ

Original text: http://www.planabc.net/2008/08/05/...

Getting Started Tutorial for Beginners⑧: Easily Create an Article Site

In my last post I talked about how to make a web p...

Detailed explanation of MySQL database transaction isolation levels

Database transaction isolation level There are 4 ...

Briefly describe the difference between Redis and MySQL

We know that MySQL is a persistent storage, store...

JavaScript Canvas draws dynamic wireframe effect

This article shares the specific code of JavaScri...

Share 8 MySQL pitfalls that you have to mention

MySQL is easy to install, fast and has rich funct...

How to position the header at the top using CSS sticky layout

Application scenarios: One of the new requirement...

Solution for importing more data from MySQL into Hive

Original derivative command: bin/sqoop import -co...

JavaScript jigsaw puzzle game

This article example shares the specific code of ...

Element avatar upload practice

This article uses the element official website an...

Linux swap partition (detailed explanation)

Table of contents linux 1. What is SWAP 2. What d...

In-depth explanation of binlog in MySQL 8.0

1 Introduction Binary log records SQL statements ...