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

Sample code for implementing honeycomb/hexagonal atlas with CSS

I don’t know why, but UI likes to design honeycom...

Steps for Vue3 to use mitt for component communication

Table of contents 1. Installation 2. Import into ...

Detailed explanation of MySQL database index

Table of contents 1. Introduction to MySQL Index ...

IE8 Developer Tools Menu Explanation

<br />This article has briefly explained the...

Why can't I see the access interface for Docker Tomcat?

Question: Is the origin server unable to find a r...

Spring Boot layered packaging Docker image practice and analysis (recommended)

Table of contents 1. Prepare the springboot proje...

BUG of odd width and height in IE6

As shown in the figure: But when viewed under IE6...

Next.js Getting Started Tutorial

Table of contents Introduction Create a Next.js p...

A brief discussion on the difference between src and href in HTML

Simply put, src means "I want to load this r...

MySQL view introduction and basic operation tutorial

Preface View is a very useful database object in ...

The difference between MySQL user management and PostgreSQL user management

1. MySQL User Management [Example 1.1] Log in to ...

Summary of React's way of creating components

Table of contents 1. Create components using func...

Vue implements drag progress bar

This article example shares the specific code of ...

SQL Server database error 5123 solution

Because I have a database tutorial based on SQL S...