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

Implementation of nginx flow control and access control

nginx traffic control Rate-limiting is a very use...

HTML table border control implementation code

Generally, when we use a table, we always give it...

How to use an image button as a reset form button

When we make a form, we often set a submit button ...

Solution for Docker Swarm external verification load balancing not taking effect

Problem Description I created three virtual machi...

Do you know the weird things in Javascript?

Our veteran predecessors have written countless c...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which mea...

13 Most Frequently Asked Vue Modifiers in Interviews

Table of contents 1. lazy 2.trim 3.number 4.stop ...

Steps to create a WEBSERVER using NODE.JS

Table of contents What is nodejs Install NodeJS H...

Essential skills for designing web front-end interfaces

[Required] UserInterface PhotoShop/Fireworks Desi...

SQL left join and right join principle and example analysis

There are two tables, and the records in table A ...

React configuration px conversion rem method

Install related dependencies npm i lib-flexible -...

Example code of Vue3 encapsulated magnifying glass component

Table of contents Component Infrastructure Purpos...