Detailed explanation of communication between hierarchical nested components in Vue front-end development

Detailed explanation of communication between hierarchical nested components in Vue front-end development

Preface

Vue parent-child components can easily pass the value of the parent component to the child component through props. If a component is nested in many layers, each layer needs to pass the value with the same props, which is troublesome and difficult to maintain.

Example

[Example] Component A uses component B, and component B uses component C. Component C needs to use component A's data text and component A's method getmethod. The code of component A is as follows:

<template>
  <div>
    <P>This is component A</P>
    <v-comb></v-comb>
  </div>
</template>
<script>
  import comB from '@/view/comB.vue'
  export default {
    name: 'comA',
    components:
      'v-comb': comB
    },
    data() {
      return {
        msg: 'I am the data in component A'
      }
    },
    provide: function() { //Inject properties and methods into child components return {
        text: this.msg,
        getMethod: function() {
          console.log('Execute the getMethod method in the root component')
        }
      }
    }
  }
</script>

Use the keyword provide to expose data and methods to child components
Component B is a child component of component A and a parent component of component C. The code is as follows

<template>
  <div>
    <div>
      <P>This is component B</P>
      <v-comc></v-comc>
    </div>
  </div>
</template>
<script>
  import comC from '@/view/comC.vue'
  export default {
    name: 'comB',
    components:
      'v-comc': comC
    }
  }
</script>

Component C is the grandchild of component A. Component C needs to use the data and methods of component A. The code is as follows:

<template>
  <div style="border:1px solid orange;color:orange;">
    <div>
      <P>This is a C component</P>
      <div>{{text}}</div>
      <button @click="getMethod">Call parent component method</button>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'comC',
    inject: ['text', 'getMethod'] //text and getMethod are the names provided by provider}
</script>

The inject keyword is used here to receive the information exposed by component A. Pay special attention here that the name received in inject: [] must be exactly the same as the name provided by provide.

Run, the interface is as shown below

insert image description here

summary

For multi-level nested component communication, Vue uses the provide & inject keywords to directly transfer values ​​from parent components to child components, which is very convenient to use. There is a strong coupling relationship between the problematic subcomponent and the parent component, and it is not recommended to use it unless it is absolutely necessary.

The above is the detailed content of the detailed explanation of the communication of hierarchical nested components in Vue front-end development. For more information about the communication of hierarchical nested components in Vue, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue uses refs to get the value process in nested components
  • The keep-alive component in Vue implements caching of multi-level nested routes
  • Detailed explanation of how to use Vue self-nested tree components
  • vue keep-alive enables individual components to survive and not be destroyed in multi-component nesting
  • Example of implementing nested subcomponents in Vue components
  • Solve the problem of nested monitoring of browser window changes in multiple components on a single Vue page
  • Use form-create to dynamically generate vue custom components and nested form components
  • Vue parent-child component nested sample code
  • Two ways to implement Vue multi-layer component nesting (test example)
  • Vue nested component parameter passing example sharing

<<:  Docker configuration Alibaba Cloud image acceleration pull implementation

>>:  How to insert Emoji expressions into MySQL

Recommend

Angular Cookie read and write operation code

Angular Cookie read and write operations, the cod...

Native JS realizes uniform motion of various sports

This article shares with you a uniform motion imp...

Detailed explanation of Nginx static file service configuration and optimization

Root directory and index file The root directive ...

Vue event's $event parameter = event value case

template <el-table :data="dataList"&...

Analysis of the reasons why Vue3 uses Proxy to implement data monitoring

Vue data two-way binding principle, but this meth...

MySQL learning tutorial clustered index

Clustering is actually relative to the InnoDB dat...

A simple example of mysql searching for data within N kilometers

According to the coefficient of pi and the radius...

HTML tags explained

HTML tags explained 1. HTML tags Tag: !DOCTYPE De...

Advantages and disadvantages of conditional comments in IE

IE's conditional comments are a proprietary (...

Linux redis-Sentinel configuration details

download Download address: https://redis.io/downl...

js to implement file upload style details

Table of contents 1. Overview 2. Parameters for c...

Alibaba Cloud ESC Server Docker Deployment of Single Node Mysql

1. Download the accelerated version of msyql dock...

How to parse the attribute interface of adding file system in Linux or Android

The first one: 1. Add key header files: #include ...

IIS 7.5 uses URL Rewrite module to achieve web page redirection

We all know that Apache can easily set rewrites f...

Docker installation tomcat dubbo-admin instance skills

1. Download the tomcat image docker pull tomcat:8...