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

Solution to 1045 error when navicat connects to mysql

When connecting to the local database, navicat fo...

About vue component switching, dynamic components, component caching

Table of contents 1. Component switching method M...

How to manually deploy war packages through tomcat9 on windows and linux

The results are different in Windows and Linux en...

Pure CSS to achieve input box placeholder animation and input verification

For more exciting content, please visit https://g...

Detailed explanation of querying JSON format fields in MySQL

During the work development process, a requiremen...

Detailed explanation of the use of Vue Smooth DnD, a draggable component of Vue

Table of contents Introduction and Demo API: Cont...

Solve the problem after adding --subnet to Docker network Create

After adding –subnet to Docker network Create, us...

Hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

WeChat applet custom tabBar step record

Table of contents 1. Introduction 2. Customize ta...

Configure VIM as a C++ development editor in Ubuntu

1. Copy the configuration file to the user enviro...

How to install mysql via yum on centos7

1. Check whether MySQL is installed yum list inst...

What you need to know about creating MySQL indexes

Table of contents Preface: 1. Create index method...