Detailed explanation of the usage of sync modifier in Vue3 parent-child component parameter transfer

Detailed explanation of the usage of sync modifier in Vue3 parent-child component parameter transfer

One-way data flow explanation

One-way data flow (the heap can be modified, the stack cannot be modified)

We all know that the data passed from parent to child is a one-way data flow, that is, the child component cannot directly modify the value passed by the parent component.

But in fact, for modifying values, the truth is: basic data types cannot be modified, complex data types do not modify the reference address (stack), and its value can be modified at will

Vue2.x usage

Define the form of the event to notify the parent component of the modification

That is the most basic usage: props+$emit

Writing method:

insert image description here

Use of .sync and update:

When the parent component passes the value, just add .sync after the variable;

The child component this.$emit("update:變量", 實參) can modify the value passed by the parent component;

Writing method:

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-BZtIWKTt-1631881175196) (Interview questions sorted out now.assets/image-20210917190128935.jpg)]

Parent-to-child, shorthand for passing multiple data

If the value passed to the child component is multiple data, you can directly include multiple data in one object with the help of v-bind ;

Note: v-bind.sync="doc";

What is passed to the child component is not a doc object;

But every attribute in the object.

Writing method:

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-3yidmVZ0-1631881175197) (Interview questions sorted out now.assets/image-20210917191523433.jpg)]

Use v-model abbreviation (strict requirements)

Parent component: v-model , Child component: The variable received must be value , and the event submitted by $emit must be input

Writing method:

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-GT15QqZz-1631881175199) (Interview questions sorted out now.assets/image-20210917201832481.jpg)]

Vue3.x usage

Using v-model , compared to 2.x, now multiple v-models can be used on a component, which is the usage of Vue2.x modifiers

Common usage

Use v-model when passing the parent component, and use emit("uodate: num", 實參) when modifying the child component

Writing method:

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-vQ1lQFal-1631881175199) (Interview questions sorted out now.assets/image-20210917193019726.jpg)]

Abbreviation

When the variable passed from the parent component to the child component is named: modelValue , you can use this method

Writing method:

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-4513r0bT-1631881175200) (Interview questions sorted out now.assets/image-20210917194125901.jpg)]

The above is the detailed content of the detailed explanation of the usage of the sync modifier in the parameter passing of Vue3 parent-child components. For more information about the parameter passing of Vue3 parent-child components, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of the difference between v-model directive and .sync modifier in Vue
  • Detailed explanation of Vue's sync modifier
  • Detailed explanation of the difference between using the .sync modifier and v-model in VUE custom components
  • Detailed explanation of the use of vue.sync modifier
  • Vue's .sync modifier example detailed explanation
  • How to understand the use of Vue's .sync modifier
  • Usage and principle analysis of .sync modifier in Vue

<<:  Detailed explanation of the implementation process of building a kernel tree in Ubuntu 12.04

>>:  The whole process of configuring hive metadata to MySQL

Recommend

Correct steps to install Nginx in Linux

Preface If you are like me, as a hard-working Jav...

Vue login function implementation

Table of contents Written in front Login Overview...

MySQL establishes efficient index example analysis

This article uses examples to describe how to cre...

MySQL tutorial thoroughly understands stored procedures

Table of contents 1. Concepts related to stored p...

A brief discussion on two methods to solve space-evenly compatibility issues

Since its launch in 2009, flex has been supported...

Some problems that may be caused by inconsistent MySQL encoding

Stored procedures and coding In MySQL stored proc...

Teach you how to use AWS server resources for free

AWS - Amazon's cloud computing service platfo...

How to open a page in an iframe

Solution: Just set the link's target attribute...

Summary of Vue component basics

Component Basics 1 Component Reuse Components are...

CSS to achieve compatible text alignment in different browsers

In the front-end layout of the form, we often nee...

Use Docker to build a Redis master-slave replication cluster

In a cluster with master-slave replication mode, ...