Basic implementation method of cross-component binding using v-model in Vue

Basic implementation method of cross-component binding using v-model in Vue

Hello everyone, today we will talk about how to use v-model to achieve the binding effect of parent-child components

1: The simple version code is as follows:

Parent component:

<template>
  <div>
  // 3: Use child components and use v-model to bind <About v-model="father"/>
  </div>
</template>

<script>
// 1: Import subcomponent import About from "./About";
export default {
// 2: Register subcomponents: {
    About,
  },
  data() {
      return {
      // Value for empty father:''
      }
  },
  //Monitor component data changes watch:{
      father(val){
          console.log(val);
      }
  }
};
</script>

Subcomponents:

<template>
  <div>
  // 2: Use v-model to bind <input type="text" v-model="son">
  </div>
</template>

<script>
export default {
    // 1: Receive information props from parent component: {
        value:{
            type:String,
            default:''
        }
    },
    data() {
        return {
        // 3: Assign empty value son:''
        }
    },
    // 4: Listen for changes watch:{
        // Assign value to son   
        value(){
        // Here this.value is the value in props
            this.son = this.value
        },
        // Pass son to the parent component son(){
            this.$emit('input',this.son)
        }
    }
}
</script>

As for why the first parameter of $emit is 'input' when the child component is passed to the parent component, those who are interested can learn about the principle of v-model implementation.

2: Let's move on to the use in the project (get the image address of the child component, pass it to the parent component, and update the image information synchronously)

Basically the same

Ⅰ: Introduce a child component into the parent component, and use v-model in the child component tag, assigning it an empty value
(The UploadImg tag is an imported subcomponent)

Ⅱ: Then use props to receive in the child component:

III: Also use v-model in the subcomponent page and assign an empty image to the data:

IV: Use watch in subcomponents to monitor their changes

Code on the decomposition diagram:

The value function assigns the passed parent component to the child component. This.value is the value in props.

value() {
      this.SonStaffPhoto = this.value
      console.log(this.SonStaffPhoto)
    }

Here is the child component function bound to v-model, which is used to pass itself to the parent component

SonStaffPhoto() {
      this.$emit('input', this.SonStaffPhoto)
    },

At this point you can assign the content you want to pass to the parent component to this.SonStaffPhoto (I assigned it to the variable used to store the image address)

V: You can also monitor changes in the parent component:

This.staffPhoto in the parent component can also be assigned the content you want to bind to it (I assigned it to the latest image variable, so that the child component image is updated and the parent component is also updated synchronously)

Summarize

This concludes this article on the basic implementation method of cross-component binding using v-model in Vue. For more relevant content on cross-component binding using v-model in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example code of vue custom component to implement v-model two-way binding data
  • The use of v-model in vue3 components and in-depth explanation
  • Detailed explanation of the difference between v-model directive and .sync modifier in Vue
  • A brief discussion on the principle of Vue's two-way event binding v-model
  • Vue uses v-model to encapsulate the entire process of el-pagination components
  • Problems and solutions encountered when using v-model to two-way bind the values ​​of parent-child components in Vue
  • Solution for Vue form input box not supporting focus and blur events
  • Detailed usage of Vue's form input component using custom events [Date component and currency component]
  • Example code for Vue form input binding
  • Vue form input formatting Chinese input method abnormality
  • Vue form input binding v-model

<<:  How to operate Linux file and folder permissions

>>:  Solve the problem of forgetting password in MySQL 5.7 under Linux

Recommend

JavaScript implements large file upload processing

Many times when we process file uploads, such as ...

Solve the pitfall of storing boolean type values ​​in localstorage

LocalStorage stores Boolean values Today, when I ...

Implementation of running springboot project with Docker

Introduction: The configuration of Docker running...

Implementation of react routing guard (routing interception)

React is different from Vue. It implements route ...

How to view and configure password expiration on Linux

With the right settings, you can force Linux user...

HTML line spacing setting methods and problems

To set the line spacing of <p></p>, us...

Implementation of pushing Docker images to Docker Hub

After the image is built successfully, it can be ...

MySQL 8.0.15 installation and configuration tutorial under Win10

What I have been learning recently involves knowl...

Detailed explanation of common commands in MySQL 8.0+

Enable remote access Enable remote access rights ...

A brief discussion on VUE uni-app custom components

1. Parent components can pass data to child compo...

Implementation of waterfall layout + dynamic rendering

Table of contents Typical waterfall website Water...

Use CSS to create 3D photo wall effect

Use CSS to create a 3D photo wall. The specific c...

Handtrack.js library for real-time monitoring of hand movements (recommended)

【Introduction】: Handtrack.js is a prototype libra...