当前位置:网站首页>Value transfer communication between components (parent to child, child to parent, brother component to value)
Value transfer communication between components (parent to child, child to parent, brother component to value)
2022-07-04 03:33:00 【CC ° fade】
List of articles
Preface
There are three kinds of value transmission communication between components
1、 Father's son
2、 Son father
3、 Sibling components pass values
One 、 Father's son
Parent component :<son :msg=“fatherMsg”></son>
Child components :props:[‘msg’]
// Parent component
<template>
<div>
<son :msg="fatherMsg"></son>
</div>
</template>
<script> import son from './son' export default {
data(){
return {
fatherMsg:' Parent component msg', } }, components:{
son } } </script>
// Child components
<template>
<div>
Messages received by subcomponents :{
{msg}}
</div>
</template>
<script> export default {
props:['msg'], } </script>
Two 、 Son father
Parent component :
<son @ Custom event name =“ Method ”></son>
Child components :
props:[‘ Custom event name ’]
this.$emit( Custom event name , Data to be transmitted )
// Parent component
<template>
<div>
<son @send="send"></son>
</div>
</template>
<script> import son from './son' export default {
components:{
son }, methods:{
send(msg){
console.log(' Message received by parent component :'+msg) }, }, } </script>
// Child components
<template>
<div>
<button @click="toFather"> Send to parent </button>
</div>
</template>
<script> export default {
props:['send'], methods:{
toFather(){
this.$emit('send',' Subcomponents msg') } }, } </script>
3、 ... and 、 Sibling components pass values
Through a transit (bus):
import Vue from ‘vue’
export default new Vue;
A Brother's biography :
import bus from ‘@/common/bus.js’
bus.$emit(‘toBorther’,this.msg)
B Brothers receive :
import bus from ‘@/common/bus.js’
bus.$on(‘toBorther’,(data)=>{
this.msg=data
})
// Parent component
<template>
<div class="about">
<!-- Sibling components pass values -->
<sister></sister>
<brother></brother>
</div>
</template>
<script> import sister from '../components/sister' import brother from '../components/brother' export default {
data(){
return{
} }, components:{
sister, brother }, } </script>
// transit (bus.js)
import Vue from 'vue'
export default new Vue;
//A Components (sister.vue)
<template>
<div>
<button @click="send"> push parameter </button>
</div>
</template>
<script> import bus from '@/common/bus.js' export default {
data(){
return{
msg:" This is a A The data of " } }, methods:{
send(){
// push parameter bus.$emit('toBorther',this.msg) } }, } </script>
//B Components (borther.vue)
<template>
<div>
{
{msg}}
</div>
</template>
<script> import bus from '@/common/bus.js' export default {
data(){
return{
msg:'', } }, mounted(){
bus.$on('toBorther',(data)=>{
// Receiving parameters this.msg=data }) }, } </script>
边栏推荐
- Recent learning fragmentation (14)
- CSCI 2134
- Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
- What kind of experience is it when the Institute earns 20000 yuan a month!
- MySQL backup notes
- Jenkins configures IP address access
- system information
- Li Chuang EDA learning notes 13: electrical network for drawing schematic diagram
- Webhook triggers Jenkins for sonar detection
- Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?
猜你喜欢
If you have just joined a new company, don't be fired because of your mistakes
Don't disagree, this is the most powerful "language" of the Internet
SQL injection (1) -- determine whether there are SQL injection vulnerabilities
Record a problem that soft deletion fails due to warehouse level error
Recursive structure
Session learning diary 1
logistic regression
Rhcsa day 3
JVM family -- heap analysis
WP collection plug-in free WordPress collection hang up plug-in
随机推荐
Résumé des outils communs et des points techniques de l'examen PMP
Learning video website
JVM family -- heap analysis
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
Enhanced for loop
[latex] production of complex tables: excel2latex and detail adjustment
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
Unity controls the selection of the previous and next characters
Formulaire day05
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
CSP drawing
潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
WP collection plug-in free WordPress collection hang up plug-in
96% of the collected traffic is prevented by bubble mart of cloud hosting
How to use STR function of C language
MySQL backup notes