Vue parent-child component mutual value transfer and call

Vue parent-child component mutual value transfer and call

1. Parent passes value to child component

Parent component:

<template>
  <div>
    <p class="father">Father component</p>
    <child :sid="id"></child>
  </div>
</template>

<script>
import child from './child'
export default {
  components:
    child
  },
  data() {
    return {
      id:'1234' // value passed from parent component to child component}
  }
}
</script>

Subcomponents:

<template>
  <div>
    <p class="child">Child component</p>
    <p class="child">The value of the parent component received is: {{ sid }}</p>
  </div>
</template>

<script>
export default {
  props:{
    sid:{
      type:String,
      default: '0'
    }
  },
  // props:["sid"],
  data() {
   return { 

   } 
 } 
} 
</script>

illustrate:

①sid is the value to be passed in the subcomponent. Remember that the sid before "=" must be consistent with the variable name to be accepted in the subcomponent.

② Use props in child components to accept incoming values. You can write them as object types, specify types and default values, or write them directly as strings.

③It can be used directly in the subcomponent, or it can be accessed using this.sid in the function

2. Child passes value to parent component

Parent component:

<template>
  <div>
    <p class="father">Father component</p>
   <p class="father">Receive the value of the child component: {{childSid}}</p>
    <child @passValue="parentPassValue"></child>
  </div>
</template>

<script>
import child from './child'
export default {
  components:
    child
  },
  data() {
    return {
      childSid:'' // Receive the value of the child component}
  },
  methods: {
    parentPassValue(data) {
      this.childSid = data;
    }
  }
}
</script>

Subcomponents:

<template>
  <div>
    <p class="child">Child component</p>
    <button @click="valueClick">Pass value</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
    }
  },
  methods: {
    valueClick() {
      this.$emit('passVaule',19)
    }
  }
}
</script>

illustrate:

① Give a method in the child component to trigger $emit. The first parameter is the function name ('passVaule') that the parent component introduces into the child component binding, and the second is the value to be passed (19)

②Bind a function in the parent component, call the function bound in the parent component, and perform a receiving operation on the value in it

3. The child calls the method in the parent component

Parent component:

<template>
  <div>
    <p class="father">Father component</p>
    <child @funValue="parentFunValue"></child>
  </div>
</template>

<script>
import child from './child'
export default {
  components:
    child
  },
  data() {
    return {
    }
  },
  methods: {
    parentFunValue() {
      console.log('The function in the parent component is called');
    }
  }
}
</script>

Subcomponents:

<template>
  <div>
    <p class="child">Child component</p>
    <button @click="funClick">Call parent component method</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
    }
  },
  methods: {
    funClick() {
      this.$emit('funVaule')
    }
  }
}
</script>

illustrate:

①This is similar to passing values ​​from child to parent, but instead of passing values, the bound function of the parent component is called.

4. Parent calls methods in child components

Parent component:

<template>
  <div>
    <p class="father">Father component</p>
    <button @click="childClick">Call child component method</button>
    <child ref="mychild"></child>
  </div>
</template>

<script>
import child from './child'
export default {
  components:
    child
  },
  data() {
    return {
    }
  },
  methods: {
    childClick() {
      this.$refs.mychild.testNum(1)
    }
  }
}
</script>

Subcomponents:

<template>
  <div>
    <p class="child">Child component</p>
</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
    }
  },
  methods: {
    testNum(data) {
      console.log('The method in the child component is called:', data);
    }
  }
}
</script>

illustrate:

① In the parent component, write ref = "mychild" in the imported child component. Mychid is the instance name defined for itself.

② Write this.refs.mychild.testNum() in the function, "testNum" is the function name defined in the child component

③The child component defines a function and lets the parent component call it

④This method can also pass values. Pass the value in the brackets and the subcomponent will receive it.

The above is the details of the mutual value transfer and calling of Vue parent-child components. For more information about the value transfer and calling of Vue parent-child components, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of Vue parent-child component value transfer and one-way data flow issues
  • Some pitfalls of Vue parent-child component value transfer
  • Vue component mounting and parent-child component value transfer example
  • Passing values ​​between parent and child components in Vue solves the problem of the mounted hook function running only once
  • Quickly understand the Vue parent-child component value transfer and the parent-child and child-parent methods
  • Detailed explanation of value transfer between parent and child components in Vue3

<<:  Tutorial on installing and configuring remote login to MySQL under Ubuntu

>>:  VMware WorkStation 14 pro installation Ubuntu 17.04 tutorial

Recommend

Tutorial on using prepare, execute and deallocate statements in MySQL

Preface MySQL officially refers to prepare, execu...

JavaScript uses setTimeout to achieve countdown effect

In order to enhance the ability to write JavaScri...

JavaScript method to detect the type of file

Table of contents 1. How to view the binary data ...

Security configuration and detection of SSL after the website enables https

It is standard for websites to enable SSL nowaday...

Understanding of web design layout

<br />A contradiction arises. In small works...

Common errors and solutions for connecting Navicat to virtual machine MySQL

Question 1 solve Start the service: service mysql...

Sample code for implementing follow ads with JavaScript

Floating ads are a very common form of advertisin...

Teach you how to use webpack to package and compile TypeScript code

TypeScript Bundling webpack integration Usually, ...

MySQL database case sensitivity issue

In MySQL, databases correspond to directories wit...

Introduction to major browsers and their kernels

Trident core: IE, MaxThon, TT, The World, 360, So...

Using iframe techniques to obtain visitor QQ implementation ideas and sample code

Today at work, a friend I added temporarily asked ...

How to clear the timer elegantly in Vue

Table of contents Preface optimization Derivative...

Some details about semicolons in JavaScript

Preface Semicolons in JavaScript are optional, an...

How to Dockerize a Python Django Application

Docker is an open source project that provides an...

Detailed explanation of VueRouter routing

Table of contents vue router 1. Understand the co...