Vue father-son value transfer, brother value transfer, child-father value transfer detailed explanation

Vue father-son value transfer, brother value transfer, child-father value transfer detailed explanation

1. Parent component passes value to child component

1. Parent component.vue

// <template> in parent component
    <div>
        <Child ref="child" :title="value"/>
    </div>
</template>    
<script>
export default {
    data() {
    	return {
    		value: 'hello world!'
    	}
    }
}
</script>

2. Subcomponent.vue

// <template> in parent component
    <div>
       <span>{{title}}</span>    
    </div>
</template>    
<script>
export default {
  props: {
    title:
      	type: String,
      	default: ''
    }
  }
}
</script>

//The title value is 'hello world!

2. Values ​​can also be transferred between sibling components through the middleware Bus

$emit Passing Values

$on take over

$off removes the transfer event

1.A component.js

this.$bus.$emit("flag",true)

2.B component.js

mounted() {
    this.$bus.$off('flag')
    this.$bus.$on('flag', data=> {
      this.flag = data
    })
  }

3. Subcomponents pass values ​​to parent components

1. Parent component.js

<template>
    <div>
        <Child ref="child" @getTitle="getTitle"/>
    </div>
</template>  
<script>
import Child from './components/Child'
export default {
  components:
  	Child 
  },
  data() {
    return {
    }
  },
  method:{
  	getTitle(data){
		console.log(data)
	}
  }
}
</script>

The print result is hello xuliting

2. Subcomponent.js

<template>
    <div>
       <span>{{title}}</span> 
    </div>
</template>    
<script>
export default {
  data() {
    return {
    title: 'hello xuliting'
    }
  },
  mounted(){
    this.getFun()
  },
  method:{
    getFun(){
     this.$emit("getTiltle",this.title)
    }
  }
}
</script>

Summarize:

The problem can also be solved by transferring methods between components. For example, the parent component is A, and the child components are B and C.

The method that parent component A calls child component B is defined as aFun, and aFun is passed to child component C.

This is the method passed to component C in the parent component

<C :a-fun="aFun" />

The reference is in component C, through props

props: {
    aFun: {
      type: Function,
      default: () => function() {}
    }
  }

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • A brief discussion on Vue3 father-son value transfer
  • Detailed explanation of Vue parent-child component value transfer and one-way data flow issues
  • Vue parent-child component mutual value transfer and call
  • Detailed explanation of Vue's seven value transfer methods
  • Some pitfalls of Vue parent-child component value transfer
  • Example of passing values ​​between vue child components and parent components

<<:  The difference between VOLUME and docker -v in Dockerfile

>>:  How many pixels should a web page be designed in?

Recommend

CSS to achieve the image hovering mouse folding effect

CSS to achieve the image hovering mouse folding e...

Detailed explanation of JDBC database link and related method encapsulation

Detailed explanation of JDBC database link and re...

CSS solution for centering elements with variable width and height

1. Horizontal center Public code: html: <div c...

Mini Program to Implement Sieve Lottery

This article example shares the specific code of ...

Learn Hyperlink A Tag

ask: I have styled the hyperlink using CSS, but i...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

Detailed explanation of the payment function code of the Vue project

1. Alipay method: Alipay method: Click Alipay to ...

About Docker security Docker-TLS encrypted communication issues

Table of contents 1. Security issues with Docker ...

Analyzing the node event loop and message queue

Table of contents What is async? Why do we need a...

Functions in TypeScript

Table of contents 1. Function definition 1.1 Func...

Introduction to Linux system swap space

Swap space is a common aspect of computing today,...

How to run Python script on Docker

First create a specific project directory for you...