Detailed explanation of the difference between v-model directive and .sync modifier in Vue

Detailed explanation of the difference between v-model directive and .sync modifier in Vue

v-model

    <!--Parent component-->
    <template>
        <!--v-model directive is syntactic sugar-->
        <Child v-model="model"></Child>
        <!-- Expanding the v-model directive is equivalent to the following code-->
        <!-- The default event for v-model binding is input, and the default prop is the value attribute-->
        <Child :value="model" @input="model = $event"></Child>
    </template>

You can also modify the default events and prop custom properties of v-model binding through the model option in the child component:

 //Subcomponent export default {
  model: {
         prop: 'checked',
         event: 'change'
     }
 }

So the equivalent operation when the corresponding parent component uses v-model is:

    <template>
        <Child :checked="model" @change="model = $event"></Child>
    <template>

v-model is usually used for form controls, because this way the component has stronger control capabilities

.sync

   <!-- Parent component -->
    <template>
        <!-- .sync was added in v2.4, it can be used to modify the properties passed to the child component -->
        <Child :xxx.sync="model"></Child>
        <!-- Equivalent to the following code -->
        <Child :xxx = "model" @update:xxx = "model = $event"></Child>
    </template>
    <!-- Child Component -->
    <input :value="xxx" @input = "$emit('update:xxx', $event.target.value)"/>

The attribute name xxx bound here can be changed, and the corresponding attribute name will also change:

    <!-- Parent component -->
    <template>
        <Child :foo.sync="model"></Child>
    </template>
    <!-- Child Component -->
    <input :value = "foo" @input = "$emit('update:foo', $event.target.value)"/>

The principle of .sync uses the $emit method of the child component to dispatch events to the parent component. Its application scenario is that the child component wants to modify the properties passed by the parent component;

The control capabilities of the .sync modifier are all in the parent component, and the event name is relatively fixed update:xxx

The two are essentially the same, there is no difference: "Listen for a trigger event" = "(val) => value = val".

The difference in subtleties

1. However, v-model corresponds to the input event of components such as input or textarea by default. If this input event is replaced in the child component, its essence is exactly the same as the .sync modifier. Relatively simple, cannot have multiple.

// Subcomponents can use custom events to replace the native input events corresponding to v-model by default, but we need to manually $emit in the subcomponent
model: {
        prop: "value",
        event: "update"
},

A component can use the .sync modifier on multiple properties, and can "two-way bind multiple "props" at the same time, unlike v-model, where a component can only have one.

Summarize the functional scenarios:

1. v-model is more about the final operation result, the result of two-way binding, the value, and a change operation.
For example: the value of the input box, the value list of the multiple-select box, the id value list of the tree structure (both ant design and element), etc...
2..sync is aimed at more various states, the mutual transmission of states, status, and an update operation.
For example: component loading status, submenu and tree structure expansion list (a kind of status), internal verification status of a form component, etc....

But there are exceptions, that is, v-model can also replace some .sync situations. This is for when the only function of this component is to switch states, and this state is the final operation value. At this time, the .sync modifier can be replaced. Using two different methods of two-way binding allows us to quickly understand the structure of the component.

You may also be interested in:
  • Detailed explanation of the usage of sync modifier in Vue3 parent-child component parameter transfer
  • 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 principles and implementation methods of Mysql account management

>>:  How to use nginx to block a specified interface (URL)

Recommend

Complete steps to use vue-router in vue3

Preface Managing routing is an essential feature ...

How to implement Nginx reverse proxy for multiple servers

Nginx reverse proxy multiple servers, which means...

How to implement nested if method in nginx

Nginx does not support nested if statements, nor ...

How to redirect to other pages in html page within two seconds

Copy code The code is as follows: <!DOCTYPE ht...

Sample code for generating QR code using js

Some time ago, the project needed to develop the ...

JavaScript to achieve simple image switching

This article shares the specific code for JavaScr...

The viewport in the meta tag controls the device screen css

Copy code The code is as follows: <meta name=&...

The most complete package.json analysis

Table of contents 1. Overview 2. Name field 3. Ve...

How to use the Linux nl command

1. Command Introduction nl (Number of Lines) adds...

MySQL 5.7 zip archive version installation tutorial

This article shares the installation tutorial of ...

Docker dynamically exposes ports to containers

View the IP address of the Container docker inspe...

Use auto.js to realize the automatic daily check-in function

Use auto.js to automate daily check-in Due to the...

MySQL common backup commands and shell backup scripts sharing

To back up multiple databases, you can use the fo...

Detailed example of using if statement in mysql stored procedure

This article uses an example to illustrate the us...

Detailed process of installing logstash in Docker

Edit docker-compose.yml and add the following con...