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

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

Windows10 mysql 8.0.12 non-installation version configuration startup method

This article shares the specific steps for config...

How to set up FTP server in CentOS7

FTP is mainly used for file transfer, and is gene...

Use of js optional chaining operator

Preface The optional chaining operator (?.) allow...

Vue implements the function of calling the mobile phone camera and album

This article shares the specific code of Vue to a...

Solution to the problem of passing values ​​between html pages

The first time I used the essay, I felt quite awkw...

The process of deploying a project to another host using Jenkins

environment Hostname ip address Serve Jenkins 192...

How to configure MySQL scheduled tasks (EVENT events) in detail

Table of contents 1. What is an event? 2. Enable ...

Using CSS to implement image frame animation and curve motion

The basic principle of all animations is to displ...

Detailed explanation of screen command usage in Linux

GUN Screen: Official website: http://www.gnu.org/...