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

Introduction to the use of MySQL pt-slave-restart tool

Table of contents When setting up a MySQL master-...

In-depth analysis of the slow query problem of MySQL Sending data

Through an example, I shared with you the solutio...

Summary of common commands for building ZooKeeper3.4 middleware under centos7

1. Download and decompress 1. Introduction to Zoo...

Some parameter descriptions of text input boxes in web design

In general guestbooks, forums and other places, t...

HTML/CSS Basics - Several precautions in HTML code writing (must read)

The warning points in this article have nothing t...

Use of Linux watch command

1. Command Introduction The watch command execute...

Nginx configuration to achieve multiple server load balancing

Nginx load balancing server: IP: 192.168.0.4 (Ngi...

Vue+video.js implements video playlist

This article shares the specific code of vue+vide...

Vue.$set failure pitfall discovery and solution

I accidentally found that Vue.$set was invalid in...

Tutorial diagram of using Jenkins for automated deployment under Windows

Today we will talk about how to use Jenkins+power...

Detailed steps to implement the Excel import function in Vue

1. Front-end-led implementation steps The first s...

Introduction to the use of html base tag target=_parent

The <base> tag specifies the default address...